View Javadoc
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 {
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);
69  
70      ClassLoader classLoader = ClassUtils.getDefaultClassLoader();
71  
72      String processPropertyPlaceHolders = element.getAttribute(ATTRIBUTE_PROCESS_PROPERTY_PLACEHOLDERS);
73      builder.addPropertyValue("processPropertyPlaceHolders",
74          !StringUtils.hasText(processPropertyPlaceHolders) || Boolean.parseBoolean(processPropertyPlaceHolders));
75      try {
76        String annotationClassName = element.getAttribute(ATTRIBUTE_ANNOTATION);
77        if (StringUtils.hasText(annotationClassName)) {
78          @SuppressWarnings("unchecked")
79          Class<? extends Annotation> annotationClass = (Class<? extends Annotation>) classLoader
80              .loadClass(annotationClassName);
81          builder.addPropertyValue("annotationClass", annotationClass);
82        }
83        String markerInterfaceClassName = element.getAttribute(ATTRIBUTE_MARKER_INTERFACE);
84        if (StringUtils.hasText(markerInterfaceClassName)) {
85          Class<?> markerInterface = classLoader.loadClass(markerInterfaceClassName);
86          builder.addPropertyValue("markerInterface", markerInterface);
87        }
88        String nameGeneratorClassName = element.getAttribute(ATTRIBUTE_NAME_GENERATOR);
89        if (StringUtils.hasText(nameGeneratorClassName)) {
90          Class<?> nameGeneratorClass = classLoader.loadClass(nameGeneratorClassName);
91          BeanNameGenerator nameGenerator = BeanUtils.instantiateClass(nameGeneratorClass, BeanNameGenerator.class);
92          builder.addPropertyValue("nameGenerator", nameGenerator);
93        }
94        String mapperFactoryBeanClassName = element.getAttribute(ATTRIBUTE_MAPPER_FACTORY_BEAN_CLASS);
95        if (StringUtils.hasText(mapperFactoryBeanClassName)) {
96          @SuppressWarnings("unchecked")
97          Class<? extends MapperFactoryBean> mapperFactoryBeanClass = (Class<? extends MapperFactoryBean>) classLoader
98              .loadClass(mapperFactoryBeanClassName);
99          builder.addPropertyValue("mapperFactoryBeanClass", mapperFactoryBeanClass);
100       }
101     } catch (Exception ex) {
102       XmlReaderContext readerContext = parserContext.getReaderContext();
103       readerContext.error(ex.getMessage(), readerContext.extractSource(element), ex.getCause());
104     }
105 
106     builder.addPropertyValue("sqlSessionTemplateBeanName", element.getAttribute(ATTRIBUTE_TEMPLATE_REF));
107     builder.addPropertyValue("sqlSessionFactoryBeanName", element.getAttribute(ATTRIBUTE_FACTORY_REF));
108     builder.addPropertyValue("lazyInitialization", element.getAttribute(ATTRIBUTE_LAZY_INITIALIZATION));
109     builder.addPropertyValue("defaultScope", element.getAttribute(ATTRIBUTE_DEFAULT_SCOPE));
110     builder.addPropertyValue("basePackage", element.getAttribute(ATTRIBUTE_BASE_PACKAGE));
111 
112     // for spring-native
113     builder.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
114 
115     return builder.getBeanDefinition();
116   }
117 
118   /**
119    * {@inheritDoc}
120    *
121    * @since 2.0.2
122    */
123   @Override
124   protected boolean shouldGenerateIdAsFallback() {
125     return true;
126   }
127 
128 }