今天寫的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類下