springspring-bootaotspring-native

How to use the Properties configuration file during spring aot processing


I want to use the configuration of MybatisProperties (corresponding to application.yml) during aop processing, but I have been unable to obtain the corresponding value

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);
        }
    }

}

How to normally read the configuration bean file for AOT processing

the problems encountered:

The mapperLocations read during the aot processing phase are empty.

desirable results:

The value of mapperLocations can be read normally during the AOT processing stage

Here is my configuration file:


mybatis:
  configuration:
    map-underscore-to-camel-case: true
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  mapper-locations: classpath:/mapper/*.xml

Solution

  • Answer here

    You can manually bind MybatisProperties through environment to obtain objects

    @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);
            }
        }
    
    }
    

    result:

    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()