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