我想在aop处理时使用MybatisProperties的配置(对应application.yml),但是一直无法获取对应的值
public class MybatisBeanFactoryInitializationAotContribution implements BeanFactoryInitializationAotContribution {
private final MybatisProperties mybatisProperties;
public MybatisBeanFactoryInitializationAotContribution(MybatisProperties mybatisProperties) {
this.mybatisProperties = mybatisProperties;
}
@Override
public void applyTo(GenerationContext generationContext, BeanFactoryInitializationCode beanFactoryInitializationCode) {
log.info("MybatisBeanFactoryInitializationAotContribution {}", mybatisProperties.getMapperLocations());
RuntimeHints hints = generationContext.getRuntimeHints();
String[] mapperLocations = mybatisProperties.getMapperLocations();
if (mapperLocations != null) {
log.info("mapperLocations-{}", mapperLocations);
Stream.of(mapperLocations).map(item -> item.replace("classpath:/", "")).forEach(hints.resources()::registerPattern);
}
}
}
如何正常读取配置bean文件进行AOT处理
遇到的问题:
aot处理阶段读取的mapperLocations为空。
理想的结果:
AOT处理阶段可以正常读取mapperLocations的值
这是我的配置文件:
mybatis:
configuration:
map-underscore-to-camel-case: true
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mapper-locations: classpath:/mapper/*.xml
可以通过环境手动绑定MybatisProperties来获取对象
@Slf4j
public class MybatisBeanFactoryInitializationAotContribution implements BeanFactoryInitializationAotContribution {
private final Environment environment;
public MybatisBeanFactoryInitializationAotContribution(Environment environment) {
this.environment = environment;
}
@Override
public void applyTo(GenerationContext generationContext, BeanFactoryInitializationCode beanFactoryInitializationCode) {
MybatisProperties mybatisProperties = Binder.get(environment)
.bind("mybatis", Bindable.of(MybatisProperties.class))
.get();
log.info("properties.getMapperLocations()");
log.info("{}", mybatisProperties.getMapperLocations());
log.info("properties.getMapperLocations()");
String[] mapperLocations = mybatisProperties.getMapperLocations();
RuntimeHints hints = generationContext.getRuntimeHints();
if (mapperLocations != null) {
log.info("mapperLocations-{}", mapperLocations);
Stream.of(mapperLocations).map(item -> item.replace("classpath:/", "")).forEach(hints.resources()::registerPattern);
}
}
}
结果:
2024-12-05 22:17:09.049 INFO 3752 --- [ main][] BeanFactoryInitializationAotContribution : properties.getMapperLocations()
2024-12-05 22:17:09.049 INFO 3752 --- [ main][] BeanFactoryInitializationAotContribution : classpath:/mapper/*.xml
2024-12-05 22:17:09.051 INFO 3752 --- [ main][] BeanFactoryInitializationAotContribution : properties.getMapperLocations()