慕课网yijun zhang老师的秒杀系统是很好的学习Spring MVC的课程.
张老师的代码是基于Xml配置的.
而Spring in action4的作者Craig Walls在书中推荐使用Java Config进行无xml的配置.
所以我借鉴书中的知识, 通过java config配置了spring相关配置, druid, mybatis, redis.
如需要完整代码的朋友, 可以私信我, 我私发给你.
在整理过程中遇到了一个很奇怪的问题, 在此记录一下, 以便增强记忆, 同时也方便遇到问题的朋友参考.
问题现象:
整合阿里巴巴druid数据库连接池组件的时候, 发现 DruidConfig里所有的@bean注解的方法都没有被执行,
然后导致所得dao相关的Bean没有初始化, 而在service层需要注入DAO的Bean时, 就报出不存在相关bean的错误.
DruidConfig的代码如下.
@Configuration
@PropertySource("classpath:config/druidConfig.properties")
public class DruidConfig {
@Value("${spring.datasource.driver-class-name}")
private String driver;
@Value("${spring.datasource.type}")
private String type;
@Value("${spring.datasource.url}")
private String url;
@Value("${spring.datasource.username}")
private String userName;
@Value("${spring.datasource.password}")
private String password;
@Value("${spring.datasource.minIdle}")
private Integer minIdle;
@Value("${spring.datasource.maxActive}")
private Integer maxActive;
@Value("${spring.datasource.initialSize}")
private Integer initialSize;
@Value("${spring.datasource.maxWait}")
private Integer maxWait;
@Value("${spring.datasource.validationQuery}")
private String validationQuery;
@Bean(name = "dataSource")
public DataSource druidDataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(driver);
dataSource.setDbType(type);
dataSource.setUrl(url);
dataSource.setUsername(userName);
dataSource.setPassword(password);
dataSource.setMaxActive(maxActive);
dataSource.setMinIdle(minIdle);
dataSource.setMaxWait(maxWait);
dataSource.setInitialSize(initialSize);
dataSource.setValidationQuery(validationQuery);
return dataSource;
}
@Bean(name = "sqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
bean.setConfigLocation(resolver.getResource("classpath:config/mybatis-config.xml"));
bean.setMapperLocations(resolver.getResources("classpath:mapper/*.xml"));
bean.setTypeAliasesPackage("pn.lyndon.entity");
return bean.getObject();
}
@Bean("txManager")
public DataSourceTransactionManager dataSourceTransactionManager(DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean
public MapperScannerConfigurer mapperScannerConfigurer() {
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setBasePackage("pn.lyndon.dao");
mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
return mapperScannerConfigurer;
}
}
通过各种调试, 包括在DruidConfig.java添加构造器debug等, 发现如果DruidConfig.java中没有mapperScanConfigurer函数的话,
DruidConfig.java文件中其他的函数就可以正常生成, dataSource, sqlSessionFactory等Bean.
最后把生成mapperScanConfigurer的Bean放到RootConfig.java中, 问题解决.
PS: 这个问题, 与生成的bean的初始化顺序有关系, 有兴趣的同学可以学习下ConfigurationClassParser的doProcessConfigurationClass函数, 了解各种注解的初始化顺序.
这方面知识可以参考: https://blog.csdn.net/andy_zhang2007/article/details/78549773