我想配置多数据源,添加一个SQL Server 数据库 ,根据 springboot-mybatis-mutil-datasource 项目的 数据源配置文件 的思路,总体代码如下
@Configuration
@MapperScan(basePackages = {"io.renren.modules.api.dao","io.renren.modules.job.dao","io.renren.modules.oss.dao","io.renren.modules.sys.dao"},sqlSessionFactoryRef = "masterSqlSessionFactory")
public class DruidConfig {
static final String MAPPER_LOCATION = "classpath:mapper/**/*.xml";
@Bean(name = "masterDataSource")
@Primary
public DataSource dataSource(){...}
@Bean(name = "masterSqlSessionFactory")
@Primary
public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource masterDataSource)throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(masterDataSource);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources(DruidConfig.MAPPER_LOCATION));
return sessionFactory.getObject();
}
}
但是遇到如下错误,无法启动
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'scheduleJobController': Unsatisfied dependency expressed through field 'scheduleJobService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scheduleJobService': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: cronExpression cannot be null
就算是注释掉io.renren.modules.job下@Configuration,@RestController,@RequestMapping
可以启动,但还是无法登录。
是思路本身有问题,还是有其他配置要做。
注:没有修改application-dev.yml文件,也没有配置第二个数据源,只是尝试一下配置是否可行