View Javadoc
1   /**
2    *    Copyright 2010-2015 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    *       http://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.annotation;
17  
18  import java.lang.annotation.Annotation;
19  import java.lang.annotation.Documented;
20  import java.lang.annotation.ElementType;
21  import java.lang.annotation.Retention;
22  import java.lang.annotation.RetentionPolicy;
23  import java.lang.annotation.Target;
24  
25  import org.mybatis.spring.mapper.MapperFactoryBean;
26  import org.mybatis.spring.mapper.MapperScannerConfigurer;
27  import org.springframework.beans.factory.support.BeanNameGenerator;
28  import org.springframework.context.annotation.Import;
29  
30  /**
31   * Use this annotation to register MyBatis mapper interfaces when using Java
32   * Config. It performs when same work as {@link MapperScannerConfigurer} via
33   * {@link MapperScannerRegistrar}.
34   *
35   * <p>Configuration example:</p>
36   * <pre class="code">
37   * &#064;Configuration
38   * &#064;MapperScan("org.mybatis.spring.sample.mapper")
39   * public class AppConfig {
40   *
41   *   &#064;Bean
42   *   public DataSource dataSource() {
43   *     return new EmbeddedDatabaseBuilder()
44   *              .addScript("schema.sql")
45   *              .build();
46   *   }
47   *
48   *   &#064;Bean
49   *   public DataSourceTransactionManager transactionManager() {
50   *     return new DataSourceTransactionManager(dataSource());
51   *   }
52   *
53   *   &#064;Bean
54   *   public SqlSessionFactory sqlSessionFactory() throws Exception {
55   *     SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
56   *     sessionFactory.setDataSource(dataSource());
57   *     return sessionFactory.getObject();
58   *   }
59   * }
60   * </pre>
61   *
62   * @author Michael Lanyon
63   * @author Eduardo Macarron
64   *
65   * @since 1.2.0
66   * @see MapperScannerRegistrar
67   * @see MapperFactoryBean
68   * @version $Id$
69   */
70  @Retention(RetentionPolicy.RUNTIME)
71  @Target(ElementType.TYPE)
72  @Documented
73  @Import(MapperScannerRegistrar.class)
74  public @interface MapperScan {
75  
76    /**
77     * Alias for the {@link #basePackages()} attribute. Allows for more concise
78     * annotation declarations e.g.:
79     * {@code @EnableMyBatisMapperScanner("org.my.pkg")} instead of {@code
80     * @EnableMyBatisMapperScanner(basePackages= "org.my.pkg"})}.
81     */
82    String[] value() default {};
83  
84    /**
85     * Base packages to scan for MyBatis interfaces. Note that only interfaces
86     * with at least one method will be registered; concrete classes will be
87     * ignored.
88     */
89    String[] basePackages() default {};
90  
91    /**
92     * Type-safe alternative to {@link #basePackages()} for specifying the packages
93     * to scan for annotated components. The package of each class specified will be scanned.
94     * <p>Consider creating a special no-op marker class or interface in each package
95     * that serves no purpose other than being referenced by this attribute.
96     */
97    Class<?>[] basePackageClasses() default {};
98  
99    /**
100    * The {@link BeanNameGenerator} class to be used for naming detected components
101    * within the Spring container.
102    */
103   Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class;
104 
105   /**
106    * This property specifies the annotation that the scanner will search for.
107    * <p>
108    * The scanner will register all interfaces in the base package that also have
109    * the specified annotation.
110    * <p>
111    * Note this can be combined with markerInterface.
112    */
113   Class<? extends Annotation> annotationClass() default Annotation.class;
114 
115   /**
116    * This property specifies the parent that the scanner will search for.
117    * <p>
118    * The scanner will register all interfaces in the base package that also have
119    * the specified interface class as a parent.
120    * <p>
121    * Note this can be combined with annotationClass.
122    */
123   Class<?> markerInterface() default Class.class;
124 
125   /**
126    * Specifies which {@code SqlSessionTemplate} to use in the case that there is
127    * more than one in the spring context. Usually this is only needed when you
128    * have more than one datasource.
129    */
130   String sqlSessionTemplateRef() default "";
131 
132   /**
133    * Specifies which {@code SqlSessionFactory} to use in the case that there is
134    * more than one in the spring context. Usually this is only needed when you
135    * have more than one datasource.
136    */
137   String sqlSessionFactoryRef() default "";
138 
139   /**
140    * Specifies a custom MapperFactoryBean to return a mybatis proxy as spring bean.
141    *
142    */
143   Class<? extends MapperFactoryBean> factoryBean() default MapperFactoryBean.class;
144 
145 }