今天写的Configuration类的@Value属性值为null

@Configuration
public class MybatisConfigurer {
@Value("${spring.datasource.url}")
private String dbUrl;
@Value("${spring.datasource.username}")
private String username;
@Value("${spring.datasource.password}")
private String password;
@Value("${spring.datasource.driver-class-name}")
private String driverClassName;
……
@Bean
public MapperScannerConfigurer mapperScannerConfigurer() {
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactoryBean");
mapperScannerConfigurer.setBasePackage("com.abc.devide.demo.mapper");
//配置通用Mapper,详情请查阅官方文档
Properties properties = new Properties();
properties.setProperty("mappers", MAPPER_INTERFACE_REFERENCE);
properties.setProperty("notEmpty", "false");//insert、update是否判断字符串类型!='' 即 test="str != null"表达式内是否追加 and str != ''
properties.setProperty("IDENTITY", "MYSQL");
mapperScannerConfigurer.setProperties(properties); return mapperScannerConfigurer;
} }

问题的原因:

  MapperScannerConfigurer实现了BeanDefinitionRegistryPostProcessor使得spring容器的执行的一些顺序问题,引起了上述@Value属性不能注入

解决方法:

  将MapperScannerConfigurer单独放在一个@Configuration类下

05-11 21:43