• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mybatis / spring / #1180

pending completion
#1180

push

github

web-flow
Update dependency maven to v3.9.3

829 of 925 relevant lines covered (89.62%)

0.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.89
/src/main/java/org/mybatis/spring/config/MapperScannerBeanDefinitionParser.java
1
/*
2
 * Copyright 2010-2023 the original author or authors.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *    https://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package org.mybatis.spring.config;
17

18
import java.lang.annotation.Annotation;
19

20
import org.mybatis.spring.mapper.ClassPathMapperScanner;
21
import org.mybatis.spring.mapper.MapperFactoryBean;
22
import org.mybatis.spring.mapper.MapperScannerConfigurer;
23
import org.springframework.beans.BeanUtils;
24
import org.springframework.beans.factory.config.BeanDefinition;
25
import org.springframework.beans.factory.support.AbstractBeanDefinition;
26
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
27
import org.springframework.beans.factory.support.BeanNameGenerator;
28
import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
29
import org.springframework.beans.factory.xml.ParserContext;
30
import org.springframework.beans.factory.xml.XmlReaderContext;
31
import org.springframework.util.ClassUtils;
32
import org.springframework.util.StringUtils;
33
import org.w3c.dom.Element;
34

35
/**
36
 * A {#code BeanDefinitionParser} that handles the element scan of the MyBatis. namespace
37
 *
38
 * @author Lishu Luo
39
 * @author Eduardo Macarron
40
 *
41
 * @since 1.2.0
42
 *
43
 * @see MapperFactoryBean
44
 * @see ClassPathMapperScanner
45
 * @see MapperScannerConfigurer
46
 */
47

48
public class MapperScannerBeanDefinitionParser extends AbstractBeanDefinitionParser {
1✔
49

50
  private static final String ATTRIBUTE_BASE_PACKAGE = "base-package";
51
  private static final String ATTRIBUTE_ANNOTATION = "annotation";
52
  private static final String ATTRIBUTE_MARKER_INTERFACE = "marker-interface";
53
  private static final String ATTRIBUTE_NAME_GENERATOR = "name-generator";
54
  private static final String ATTRIBUTE_TEMPLATE_REF = "template-ref";
55
  private static final String ATTRIBUTE_FACTORY_REF = "factory-ref";
56
  private static final String ATTRIBUTE_MAPPER_FACTORY_BEAN_CLASS = "mapper-factory-bean-class";
57
  private static final String ATTRIBUTE_LAZY_INITIALIZATION = "lazy-initialization";
58
  private static final String ATTRIBUTE_DEFAULT_SCOPE = "default-scope";
59
  private static final String ATTRIBUTE_PROCESS_PROPERTY_PLACEHOLDERS = "process-property-placeholders";
60

61
  /**
62
   * {@inheritDoc}
63
   *
64
   * @since 2.0.2
65
   */
66
  @Override
67
  protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
68
    BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(MapperScannerConfigurer.class);
1✔
69

70
    ClassLoader classLoader = ClassUtils.getDefaultClassLoader();
1✔
71

72
    String processPropertyPlaceHolders = element.getAttribute(ATTRIBUTE_PROCESS_PROPERTY_PLACEHOLDERS);
1✔
73
    builder.addPropertyValue("processPropertyPlaceHolders",
1✔
74
        !StringUtils.hasText(processPropertyPlaceHolders) || Boolean.parseBoolean(processPropertyPlaceHolders));
1✔
75
    try {
76
      String annotationClassName = element.getAttribute(ATTRIBUTE_ANNOTATION);
1✔
77
      if (StringUtils.hasText(annotationClassName)) {
1✔
78
        @SuppressWarnings("unchecked")
79
        Class<? extends Annotation> annotationClass = (Class<? extends Annotation>) classLoader
1✔
80
            .loadClass(annotationClassName);
1✔
81
        builder.addPropertyValue("annotationClass", annotationClass);
1✔
82
      }
83
      String markerInterfaceClassName = element.getAttribute(ATTRIBUTE_MARKER_INTERFACE);
1✔
84
      if (StringUtils.hasText(markerInterfaceClassName)) {
1✔
85
        Class<?> markerInterface = classLoader.loadClass(markerInterfaceClassName);
1✔
86
        builder.addPropertyValue("markerInterface", markerInterface);
1✔
87
      }
88
      String nameGeneratorClassName = element.getAttribute(ATTRIBUTE_NAME_GENERATOR);
1✔
89
      if (StringUtils.hasText(nameGeneratorClassName)) {
1✔
90
        Class<?> nameGeneratorClass = classLoader.loadClass(nameGeneratorClassName);
1✔
91
        BeanNameGenerator nameGenerator = BeanUtils.instantiateClass(nameGeneratorClass, BeanNameGenerator.class);
1✔
92
        builder.addPropertyValue("nameGenerator", nameGenerator);
1✔
93
      }
94
      String mapperFactoryBeanClassName = element.getAttribute(ATTRIBUTE_MAPPER_FACTORY_BEAN_CLASS);
1✔
95
      if (StringUtils.hasText(mapperFactoryBeanClassName)) {
1✔
96
        @SuppressWarnings("unchecked")
97
        Class<? extends MapperFactoryBean> mapperFactoryBeanClass = (Class<? extends MapperFactoryBean>) classLoader
1✔
98
            .loadClass(mapperFactoryBeanClassName);
1✔
99
        builder.addPropertyValue("mapperFactoryBeanClass", mapperFactoryBeanClass);
1✔
100
      }
101
    } catch (Exception ex) {
×
102
      XmlReaderContext readerContext = parserContext.getReaderContext();
×
103
      readerContext.error(ex.getMessage(), readerContext.extractSource(element), ex.getCause());
×
104
    }
1✔
105

106
    builder.addPropertyValue("sqlSessionTemplateBeanName", element.getAttribute(ATTRIBUTE_TEMPLATE_REF));
1✔
107
    builder.addPropertyValue("sqlSessionFactoryBeanName", element.getAttribute(ATTRIBUTE_FACTORY_REF));
1✔
108
    builder.addPropertyValue("lazyInitialization", element.getAttribute(ATTRIBUTE_LAZY_INITIALIZATION));
1✔
109
    builder.addPropertyValue("defaultScope", element.getAttribute(ATTRIBUTE_DEFAULT_SCOPE));
1✔
110
    builder.addPropertyValue("basePackage", element.getAttribute(ATTRIBUTE_BASE_PACKAGE));
1✔
111

112
    // for spring-native
113
    builder.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
1✔
114

115
    return builder.getBeanDefinition();
1✔
116
  }
117

118
  /**
119
   * {@inheritDoc}
120
   *
121
   * @since 2.0.2
122
   */
123
  @Override
124
  protected boolean shouldGenerateIdAsFallback() {
125
    return true;
1✔
126
  }
127

128
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc