|
19 | 19 | import java.lang.reflect.Method; |
20 | 20 | import java.lang.reflect.ParameterizedType; |
21 | 21 | import java.lang.reflect.Type; |
22 | | -import java.util.HashSet; |
23 | 22 | import java.util.Map; |
24 | 23 | import java.util.Set; |
25 | 24 | import java.util.function.Function; |
26 | 25 | import java.util.stream.Collectors; |
27 | 26 | import java.util.stream.Stream; |
28 | | -import org.apache.commons.logging.LogFactory; |
29 | 27 | import org.apache.ibatis.annotations.DeleteProvider; |
30 | 28 | import org.apache.ibatis.annotations.InsertProvider; |
31 | 29 | import org.apache.ibatis.annotations.SelectProvider; |
32 | 30 | import org.apache.ibatis.annotations.UpdateProvider; |
33 | 31 | import org.apache.ibatis.reflection.TypeParameterResolver; |
34 | 32 | import org.mybatis.spring.mapper.MapperFactoryBean; |
35 | | -import org.mybatis.spring.mapper.MapperScannerConfigurer; |
36 | 33 | import org.springframework.aot.hint.MemberCategory; |
37 | 34 | import org.springframework.aot.hint.RuntimeHints; |
38 | 35 | import org.springframework.beans.PropertyValue; |
39 | | -import org.springframework.beans.factory.BeanFactory; |
40 | | -import org.springframework.beans.factory.BeanFactoryAware; |
41 | 36 | import org.springframework.beans.factory.aot.BeanFactoryInitializationAotContribution; |
42 | 37 | import org.springframework.beans.factory.aot.BeanFactoryInitializationAotProcessor; |
43 | | -import org.springframework.beans.factory.aot.BeanRegistrationExcludeFilter; |
44 | 38 | import org.springframework.beans.factory.config.BeanDefinition; |
45 | | -import org.springframework.beans.factory.config.ConfigurableBeanFactory; |
46 | 39 | import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; |
47 | | -import org.springframework.beans.factory.config.ConstructorArgumentValues; |
48 | | -import org.springframework.beans.factory.support.MergedBeanDefinitionPostProcessor; |
49 | | -import org.springframework.beans.factory.support.RegisteredBean; |
50 | | -import org.springframework.beans.factory.support.RootBeanDefinition; |
51 | 40 | import org.springframework.context.annotation.Bean; |
52 | 41 | import org.springframework.context.annotation.Configuration; |
53 | | -import org.springframework.core.ResolvableType; |
54 | | -import org.springframework.util.ClassUtils; |
55 | 42 | import org.springframework.util.ReflectionUtils; |
56 | 43 |
|
57 | 44 | /** |
58 | 45 | * MyBatis 的 Spring AOT 集成。 |
59 | 46 | * |
60 | | - * <p>取自 MyBatis 官方的 MyBatisNativeConfiguration 模板;静态的 GraalVM 元数据(日志实现、 缓存装饰器、dtd/xsd 资源等)已按依赖分目录改为 |
61 | | - * {@code META-INF/native-image/<groupId>/<artifactId>/<version>/reachability-metadata.json}, |
62 | | - * 这里只保留无法用静态元数据表达的部分: |
| 47 | + * <p>取自 MyBatis 官方的 MyBatisNativeConfiguration 模板;静态的 GraalVM 元数据(日志实现、缓存装饰器、dtd/xsd 资源等)已按依赖分目录改为 |
| 48 | + * {@code META-INF/native-image/<groupId>/<artifactId>/<version>/reachability-metadata.json}。 |
63 | 49 | * |
64 | | - * <ul> |
65 | | - * <li>{@code MyBatisMapperFactoryBeanPostProcessor}:为 MapperFactoryBean 的 BeanDefinition |
66 | | - * 补齐类型信息,避免 AOT 代码生成退回到自动装配 Class; |
67 | | - * <li>{@code MyBatisBeanFactoryInitializationAotProcessor}:处理 mapper 扫描器生成的 MapperFactoryBean |
68 | | - * 定义,并为 mapper 接口及其实体注册反射(由 AOT 扫描结果驱动)。 |
69 | | - * </ul> |
| 50 | + * <p>模板中的 {@code MyBatisMapperFactoryBeanPostProcessor} 与对 {@code MapperScannerConfigurer} 的 AOT |
| 51 | + * 排除已删除:mybatis-spring 4.1.0 起扫描器直接以 {@code Class} 登记 mapper 构造参数(mybatis/spring#1238),并在使用 AOT |
| 52 | + * 产物运行时跳过重复扫描(mybatis/spring#1239)。该后处理器补齐的泛型 targetType 随之不再生成,native 运行时按类型匹配时会先实例化 {@code |
| 53 | + * MapperFactoryBean}(只调用构造器)以取得 mapper 类型,nativeTest 实测无影响。 |
| 54 | + * |
| 55 | + * <p>这里只保留 mybatis-spring 尚未内置、又无法用静态元数据表达的部分:{@code MyBatisBeanFactoryInitializationAotProcessor} |
| 56 | + * 按 AOT 扫描结果为 mapper 接口注册反射、JDK 代理与同名 XML 资源,并为其方法的参数、返回值及 SQL Provider 类型注册反射。 |
70 | 57 | */ |
71 | 58 | @Configuration(proxyBeanMethods = false) |
72 | 59 | public class MyBatisNativeConfiguration { |
73 | 60 |
|
74 | | - @Bean |
75 | | - static MyBatisMapperFactoryBeanPostProcessor myBatisMapperFactoryBeanPostProcessor() { |
76 | | - return new MyBatisMapperFactoryBeanPostProcessor(); |
77 | | - } |
78 | | - |
79 | 61 | @Bean |
80 | 62 | MyBatisBeanFactoryInitializationAotProcessor myBatisBeanFactoryInitializationAotProcessor() { |
81 | 63 | return new MyBatisBeanFactoryInitializationAotProcessor(); |
82 | 64 | } |
83 | 65 |
|
84 | 66 | static class MyBatisBeanFactoryInitializationAotProcessor |
85 | | - implements BeanFactoryInitializationAotProcessor, BeanRegistrationExcludeFilter { |
86 | | - |
87 | | - private final Set<Class<?>> excludeClasses = new HashSet<>(); |
88 | | - |
89 | | - MyBatisBeanFactoryInitializationAotProcessor() { |
90 | | - excludeClasses.add(MapperScannerConfigurer.class); |
91 | | - } |
92 | | - |
93 | | - @Override |
94 | | - public boolean isExcludedFromAotProcessing(RegisteredBean registeredBean) { |
95 | | - return excludeClasses.contains(registeredBean.getBeanClass()); |
96 | | - } |
| 67 | + implements BeanFactoryInitializationAotProcessor { |
97 | 68 |
|
98 | 69 | @Override |
99 | 70 | public BeanFactoryInitializationAotContribution processAheadOfTime( |
@@ -228,63 +199,4 @@ private static Class<?> typeToClass(Type src, Class<?> fallback) { |
228 | 199 | return result; |
229 | 200 | } |
230 | 201 | } |
231 | | - |
232 | | - static class MyBatisMapperFactoryBeanPostProcessor |
233 | | - implements MergedBeanDefinitionPostProcessor, BeanFactoryAware { |
234 | | - |
235 | | - private static final org.apache.commons.logging.Log LOG = |
236 | | - LogFactory.getLog(MyBatisMapperFactoryBeanPostProcessor.class); |
237 | | - |
238 | | - private static final String MAPPER_FACTORY_BEAN = |
239 | | - "org.mybatis.spring.mapper.MapperFactoryBean"; |
240 | | - |
241 | | - private ConfigurableBeanFactory beanFactory; |
242 | | - |
243 | | - @Override |
244 | | - public void setBeanFactory(BeanFactory beanFactory) { |
245 | | - this.beanFactory = (ConfigurableBeanFactory) beanFactory; |
246 | | - } |
247 | | - |
248 | | - @Override |
249 | | - public void postProcessMergedBeanDefinition( |
250 | | - RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) { |
251 | | - if (ClassUtils.isPresent(MAPPER_FACTORY_BEAN, this.beanFactory.getBeanClassLoader())) { |
252 | | - resolveMapperFactoryBeanTypeIfNecessary(beanDefinition); |
253 | | - } |
254 | | - } |
255 | | - |
256 | | - /** |
257 | | - * 为 {@code MapperFactoryBean} 的 BeanDefinition 补齐类型信息。 |
258 | | - * |
259 | | - * <p>向上下文暴露泛型类型信息以避免过早初始化;同时把构造参数重新注册为 {@code Class} 字面量——扫描器只会记录 mapper 类名字符串,这会使 AOT |
260 | | - * 代码生成退回到 自动装配 {@code MapperFactoryBean} 构造器,native 测试下会尝试自动装配 {@code Class<?>}。 |
261 | | - */ |
262 | | - private void resolveMapperFactoryBeanTypeIfNecessary(RootBeanDefinition beanDefinition) { |
263 | | - if (!beanDefinition.hasBeanClass() |
264 | | - || !MapperFactoryBean.class.isAssignableFrom(beanDefinition.getBeanClass())) { |
265 | | - return; |
266 | | - } |
267 | | - if (beanDefinition.getResolvableType().hasUnresolvableGenerics()) { |
268 | | - Class<?> mapperInterface = getMapperInterface(beanDefinition); |
269 | | - if (mapperInterface != null) { |
270 | | - beanDefinition.setTargetType( |
271 | | - ResolvableType.forClassWithGenerics( |
272 | | - beanDefinition.getBeanClass(), mapperInterface)); |
273 | | - ConstructorArgumentValues constructorArgumentValues = |
274 | | - new ConstructorArgumentValues(); |
275 | | - constructorArgumentValues.addGenericArgumentValue(mapperInterface); |
276 | | - beanDefinition.setConstructorArgumentValues(constructorArgumentValues); |
277 | | - } |
278 | | - } |
279 | | - } |
280 | | - |
281 | | - private Class<?> getMapperInterface(RootBeanDefinition beanDefinition) { |
282 | | - try { |
283 | | - return (Class<?>) beanDefinition.getPropertyValues().get("mapperInterface"); |
284 | | - } catch (Exception e) { |
285 | | - LOG.debug("Fail getting mapper interface type.", e); |
286 | | - return null; |
287 | | - } |
288 | | - } |
289 | | - } |
290 | 202 | } |
0 commit comments