|
61 | 61 | * {@link EnableAutoConfiguration Auto-Configuration} for Mybatis. Contributes a
|
62 | 62 | * {@link SqlSessionFactory} and a {@link SqlSessionTemplate}.
|
63 | 63 | *
|
64 |
| - * If {@link org.mybatis.spring.annotation.MapperScan} is used, or a configuration file is |
65 |
| - * specified as a property, those will be considered, otherwise this auto-configuration |
66 |
| - * will attempt to register mappers based on the interface definitions in or under the |
67 |
| - * root auto-configuration package. |
| 64 | + * If {@link org.mybatis.spring.annotation.MapperScan} is used, or a |
| 65 | + * configuration file is specified as a property, those will be considered, |
| 66 | + * otherwise this auto-configuration will attempt to register mappers based on |
| 67 | + * the interface definitions in or under the root auto-configuration package. |
68 | 68 | *
|
69 | 69 | * @author Eddú Meléndez
|
70 | 70 | * @author Josh Long
|
|
78 | 78 | @AutoConfigureAfter(DataSourceAutoConfiguration.class)
|
79 | 79 | public class MybatisAutoConfiguration {
|
80 | 80 |
|
81 |
| - private static Log log = LogFactory.getLog(MybatisAutoConfiguration.class); |
82 |
| - |
83 |
| - @Autowired |
84 |
| - private MybatisProperties properties; |
85 |
| - |
86 |
| - @Autowired(required = false) |
87 |
| - private Interceptor[] interceptors; |
88 |
| - |
89 |
| - @Autowired |
90 |
| - private ResourceLoader resourceLoader = new DefaultResourceLoader(); |
91 |
| - |
92 |
| - @Autowired(required = false) |
93 |
| - private DatabaseIdProvider databaseIdProvider; |
94 |
| - |
95 |
| - @PostConstruct |
96 |
| - public void checkConfigFileExists() { |
97 |
| - if (this.properties.isCheckConfigLocation()) { |
98 |
| - Resource resource = this.resourceLoader.getResource(this.properties.getConfigLocation()); |
99 |
| - Assert.state(resource.exists(), "Cannot find config location: " + resource |
100 |
| - + " (please add config file or check your Mybatis " |
101 |
| - + "configuration)"); |
102 |
| - } |
103 |
| - } |
104 |
| - |
105 |
| - @Bean |
106 |
| - @ConditionalOnMissingBean |
107 |
| - public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { |
108 |
| - SqlSessionFactoryBean factory = new SqlSessionFactoryBean(); |
109 |
| - factory.setDataSource(dataSource); |
110 |
| - factory.setVfs(SpringBootVFS.class); |
111 |
| - if (StringUtils.hasText(this.properties.getConfigLocation())) { |
112 |
| - factory.setConfigLocation(this.resourceLoader.getResource(this.properties.getConfigLocation())); |
113 |
| - } |
114 |
| - factory.setConfiguration(properties.getConfiguration()); |
115 |
| - if (!ObjectUtils.isEmpty(this.interceptors)) { |
116 |
| - factory.setPlugins(this.interceptors); |
117 |
| - } |
118 |
| - if (this.databaseIdProvider != null) { |
119 |
| - factory.setDatabaseIdProvider(this.databaseIdProvider); |
120 |
| - } |
121 |
| - if (StringUtils.hasLength(this.properties.getTypeAliasesPackage())) { |
122 |
| - factory.setTypeAliasesPackage(this.properties.getTypeAliasesPackage()); |
123 |
| - } |
124 |
| - if (StringUtils.hasLength(this.properties.getTypeHandlersPackage())) { |
125 |
| - factory.setTypeHandlersPackage(this.properties.getTypeHandlersPackage()); |
126 |
| - } |
127 |
| - if (!ObjectUtils.isEmpty(this.properties.resolveMapperLocations())) { |
128 |
| - factory.setMapperLocations(this.properties.resolveMapperLocations()); |
129 |
| - } |
130 |
| - |
131 |
| - return factory.getObject(); |
132 |
| - } |
133 |
| - |
134 |
| - @Bean |
135 |
| - @ConditionalOnMissingBean |
136 |
| - public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) { |
137 |
| - ExecutorType executorType = this.properties.getExecutorType(); |
138 |
| - if (executorType != null) { |
139 |
| - return new SqlSessionTemplate(sqlSessionFactory, executorType); |
140 |
| - } else { |
141 |
| - return new SqlSessionTemplate(sqlSessionFactory); |
142 |
| - } |
143 |
| - } |
144 |
| - |
145 |
| - /** |
146 |
| - * This will just scan the same base package as Spring Boot does. If you want more |
147 |
| - * power, you can explicitly use {@link org.mybatis.spring.annotation.MapperScan} but |
148 |
| - * this will get typed mappers working correctly, out-of-the-box, similar to using |
149 |
| - * Spring Data JPA repositories. |
150 |
| - */ |
151 |
| - public static class AutoConfiguredMapperScannerRegistrar implements BeanFactoryAware, |
152 |
| - ImportBeanDefinitionRegistrar, ResourceLoaderAware { |
153 |
| - |
154 |
| - private BeanFactory beanFactory; |
155 |
| - |
156 |
| - private ResourceLoader resourceLoader; |
157 |
| - |
158 |
| - @Override |
159 |
| - public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { |
160 |
| - |
161 |
| - log.debug("Searching for mappers annotated with @Mapper'"); |
162 |
| - |
163 |
| - ClassPathMapperScanner scanner = new ClassPathMapperScanner(registry); |
164 |
| - |
165 |
| - try { |
166 |
| - if (this.resourceLoader != null) { |
167 |
| - scanner.setResourceLoader(this.resourceLoader); |
168 |
| - } |
169 |
| - |
170 |
| - List<String> pkgs = AutoConfigurationPackages.get(this.beanFactory); |
171 |
| - for (String pkg : pkgs) { |
172 |
| - log.debug("Using auto-configuration base package '" + pkg + "'"); |
173 |
| - } |
174 |
| - |
175 |
| - scanner.setAnnotationClass(Mapper.class); |
176 |
| - scanner.registerFilters(); |
177 |
| - scanner.doScan(StringUtils.toStringArray(pkgs)); |
178 |
| - } catch (IllegalStateException ex) { |
179 |
| - log.debug("Could not determine auto-configuration " |
180 |
| - + "package, automatic mapper scanning disabled."); |
181 |
| - } |
182 |
| - } |
183 |
| - |
184 |
| - @Override |
185 |
| - public void setBeanFactory(BeanFactory beanFactory) throws BeansException { |
186 |
| - this.beanFactory = beanFactory; |
187 |
| - } |
188 |
| - |
189 |
| - @Override |
190 |
| - public void setResourceLoader(ResourceLoader resourceLoader) { |
191 |
| - this.resourceLoader = resourceLoader; |
192 |
| - } |
193 |
| - } |
194 |
| - |
195 |
| - /** |
196 |
| - * {@link org.mybatis.spring.annotation.MapperScan} ultimately ends up creating |
197 |
| - * instances of {@link MapperFactoryBean}. If |
198 |
| - * {@link org.mybatis.spring.annotation.MapperScan} is used then this |
199 |
| - * auto-configuration is not needed. If it is _not_ used, however, then this will |
200 |
| - * bring in a bean registrar and automatically register components based on the same |
201 |
| - * component-scanning path as Spring Boot itself. |
202 |
| - */ |
203 |
| - @Configuration |
204 |
| - @Import({ AutoConfiguredMapperScannerRegistrar.class }) |
205 |
| - @ConditionalOnMissingBean(MapperFactoryBean.class) |
206 |
| - public static class MapperScannerRegistrarNotFoundConfiguration { |
207 |
| - |
208 |
| - @PostConstruct |
209 |
| - public void afterPropertiesSet() { |
210 |
| - log.debug(String.format("No %s found.", MapperFactoryBean.class.getName())); |
211 |
| - } |
212 |
| - } |
| 81 | + private static Log log = LogFactory.getLog(MybatisAutoConfiguration.class); |
| 82 | + |
| 83 | + @Autowired |
| 84 | + private MybatisProperties properties; |
| 85 | + |
| 86 | + @Autowired(required = false) |
| 87 | + private Interceptor[] interceptors; |
| 88 | + |
| 89 | + @Autowired |
| 90 | + private ResourceLoader resourceLoader = new DefaultResourceLoader(); |
| 91 | + |
| 92 | + @Autowired(required = false) |
| 93 | + private DatabaseIdProvider databaseIdProvider; |
| 94 | + |
| 95 | + @PostConstruct |
| 96 | + public void checkConfigFileExists() { |
| 97 | + if (this.properties.isCheckConfigLocation()) { |
| 98 | + Resource resource = this.resourceLoader.getResource(this.properties.getConfigLocation()); |
| 99 | + Assert.state(resource.exists(), "Cannot find config location: " + resource |
| 100 | + + " (please add config file or check your Mybatis " + "configuration)"); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + @Bean |
| 105 | + @ConditionalOnMissingBean |
| 106 | + public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { |
| 107 | + SqlSessionFactoryBean factory = new SqlSessionFactoryBean(); |
| 108 | + factory.setDataSource(dataSource); |
| 109 | + factory.setVfs(SpringBootVFS.class); |
| 110 | + if (StringUtils.hasText(this.properties.getConfigLocation())) { |
| 111 | + factory.setConfigLocation(this.resourceLoader.getResource(this.properties.getConfigLocation())); |
| 112 | + } |
| 113 | + factory.setConfiguration(properties.getConfiguration()); |
| 114 | + if (!ObjectUtils.isEmpty(this.interceptors)) { |
| 115 | + factory.setPlugins(this.interceptors); |
| 116 | + } |
| 117 | + if (this.databaseIdProvider != null) { |
| 118 | + factory.setDatabaseIdProvider(this.databaseIdProvider); |
| 119 | + } |
| 120 | + if (StringUtils.hasLength(this.properties.getTypeAliasesPackage())) { |
| 121 | + factory.setTypeAliasesPackage(this.properties.getTypeAliasesPackage()); |
| 122 | + } |
| 123 | + if (StringUtils.hasLength(this.properties.getTypeHandlersPackage())) { |
| 124 | + factory.setTypeHandlersPackage(this.properties.getTypeHandlersPackage()); |
| 125 | + } |
| 126 | + if (!ObjectUtils.isEmpty(this.properties.resolveMapperLocations())) { |
| 127 | + factory.setMapperLocations(this.properties.resolveMapperLocations()); |
| 128 | + } |
| 129 | + |
| 130 | + return factory.getObject(); |
| 131 | + } |
| 132 | + |
| 133 | + @Bean |
| 134 | + @ConditionalOnMissingBean |
| 135 | + public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) { |
| 136 | + ExecutorType executorType = this.properties.getExecutorType(); |
| 137 | + if (executorType != null) { |
| 138 | + return new SqlSessionTemplate(sqlSessionFactory, executorType); |
| 139 | + } else { |
| 140 | + return new SqlSessionTemplate(sqlSessionFactory); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * This will just scan the same base package as Spring Boot does. If you want |
| 146 | + * more power, you can explicitly use |
| 147 | + * {@link org.mybatis.spring.annotation.MapperScan} but this will get typed |
| 148 | + * mappers working correctly, out-of-the-box, similar to using Spring Data JPA |
| 149 | + * repositories. |
| 150 | + */ |
| 151 | + public static class AutoConfiguredMapperScannerRegistrar |
| 152 | + implements BeanFactoryAware, ImportBeanDefinitionRegistrar, ResourceLoaderAware { |
| 153 | + |
| 154 | + private BeanFactory beanFactory; |
| 155 | + |
| 156 | + private ResourceLoader resourceLoader; |
| 157 | + |
| 158 | + @Override |
| 159 | + public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { |
| 160 | + |
| 161 | + log.debug("Searching for mappers annotated with @Mapper'"); |
| 162 | + |
| 163 | + ClassPathMapperScanner scanner = new ClassPathMapperScanner(registry); |
| 164 | + |
| 165 | + try { |
| 166 | + if (this.resourceLoader != null) { |
| 167 | + scanner.setResourceLoader(this.resourceLoader); |
| 168 | + } |
| 169 | + |
| 170 | + List<String> pkgs = AutoConfigurationPackages.get(this.beanFactory); |
| 171 | + for (String pkg : pkgs) { |
| 172 | + log.debug("Using auto-configuration base package '" + pkg + "'"); |
| 173 | + } |
| 174 | + |
| 175 | + scanner.setAnnotationClass(Mapper.class); |
| 176 | + scanner.registerFilters(); |
| 177 | + scanner.doScan(StringUtils.toStringArray(pkgs)); |
| 178 | + } catch (IllegalStateException ex) { |
| 179 | + log.debug("Could not determine auto-configuration " + "package, automatic mapper scanning disabled."); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + @Override |
| 184 | + public void setBeanFactory(BeanFactory beanFactory) throws BeansException { |
| 185 | + this.beanFactory = beanFactory; |
| 186 | + } |
| 187 | + |
| 188 | + @Override |
| 189 | + public void setResourceLoader(ResourceLoader resourceLoader) { |
| 190 | + this.resourceLoader = resourceLoader; |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + /** |
| 195 | + * {@link org.mybatis.spring.annotation.MapperScan} ultimately ends up |
| 196 | + * creating instances of {@link MapperFactoryBean}. If |
| 197 | + * {@link org.mybatis.spring.annotation.MapperScan} is used then this |
| 198 | + * auto-configuration is not needed. If it is _not_ used, however, then this |
| 199 | + * will bring in a bean registrar and automatically register components based |
| 200 | + * on the same component-scanning path as Spring Boot itself. |
| 201 | + */ |
| 202 | + @Configuration |
| 203 | + @Import({ AutoConfiguredMapperScannerRegistrar.class }) |
| 204 | + @ConditionalOnMissingBean(MapperFactoryBean.class) |
| 205 | + public static class MapperScannerRegistrarNotFoundConfiguration { |
| 206 | + |
| 207 | + @PostConstruct |
| 208 | + public void afterPropertiesSet() { |
| 209 | + log.debug(String.format("No %s found.", MapperFactoryBean.class.getName())); |
| 210 | + } |
| 211 | + } |
213 | 212 |
|
214 | 213 | }
|
0 commit comments