@Configuration中@Autowired private DataSource rabbitProducerDataSource;
@Slf4j
@Configuration
@PropertySource({"classpath:rabbit-producer-message.properties"})
public class RabbitProducerDataSourceConfiguration {
/**赋值*/
@Value("${rabbit.producer.druid.type}")
private Class<? extends DataSource> dataSourceType;
/**给DataSource rabbitProducerDataSource 起个名字*/
@Bean(name ="rabbitProducerDataSource")
/*
Spring boot有多个数据源时, 必须有一个是primary的, 否则无法启动.
一般我们设置某个bean为primary时, 是添加primary注解
*/
@ConfigurationProperties(prefix = "rabbit.producer.druid.jdbc")
public DataSource rabbitProducerDataSource() throws SQLException {
DataSource rabbitProducerDataSource = DataSourceBuilder.create().type(dataSourceType).build();
log.info("============= rabbitProducerDataSource : {} ================", rabbitProducerDataSource);
return rabbitProducerDataSource;
}
public DataSourceProperties primaryDataSourceProperties(){
return new DataSourceProperties();
}
/**用于备份*/
public DataSource primaryDataSource(){
return primaryDataSourceProperties().initializeDataSourceBuilder().build();
}
}
@Configuration
@AutoConfigureAfter(value = {RabbitProducerDataSourceConfiguration.class})
public class RabbitProducerMyBatisConfiguration {
@Resource(name= "rabbitProducerDataSource")
private DataSource rabbitProducerDataSource;
@Bean(name="rabbitProducerSqlSessionFactory") //自动注入上面 private DataSource rabbitProducerDataSource 入参
public SqlSessionFactory rabbitProducerSqlSessionFactory(DataSource rabbitProducerDataSource) {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(rabbitProducerDataSource);
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
try {
bean.setMapperLocations(resolver.getResources("classpath:mapper/*.xml"));
SqlSessionFactory sqlSessionFactory = bean.getObject();
if (sqlSessionFactory != null) {
sqlSessionFactory.getConfiguration().setCacheEnabled(Boolean.TRUE);
}
return sqlSessionFactory;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Bean(name="rabbitProducerSqlSessionTemplate")
public SqlSessionTemplate rabbitProducerSqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
@Configuration
public class BrokerMessageConfiguration {
@Autowired
private DataSource rabbitProducerDataSource;
/**sql 脚本*/
@Value("classpath:rabbit-producer-message-schema.sql")
private Resource schemaScript;
@Bean
public DataSourceInitializer initDataSourceInitializer() {
System.err.println("--------------rabbitProducerDataSource-----------:" + rabbitProducerDataSource);
final DataSourceInitializer initializer = new DataSourceInitializer();
//set数据源 创建jdbc 连接
initializer.setDataSource(rabbitProducerDataSource);
//执行脚本sql语句 (填充器)
initializer.setDatabasePopulator(databasePopulator());
return initializer;
}
private DatabasePopulator databasePopulator() {
final ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
populator.addScript(schemaScript);
return populator;
}
}
这里有三个@Configuration修饰的类,
@Configuration
@AutoConfigureAfter(value = {RabbitProducerDataSourceConfiguration.class})
public class RabbitProducerMyBatisConfiguration {
//此类在 加载前有条件 @AutoConfigureAfter(value = {RabbitProducerDataSourceConfiguration.class})
要等到这个配置类先加载 然后在执行 这样能保证 @Resource(name= "rabbitProducerDataSource") 可以被注入
@Resource(name= "rabbitProducerDataSource")
private DataSource rabbitProducerDataSource;
....
....
...
}
@Configuration
public class BrokerMessageConfiguration {
//此类为什么不加 @AutoConfigureAfter(value = {RabbitProducerDataSourceConfiguration.class})
能保证@Autowired 注入的数据源是 RabbitProducerDataSourceConfiguration 这个类声明的
@Bean(name ="rabbitProducerDataSource")
@Autowired
private DataSource rabbitProducerDataSource;
}
还请希望详细解答一下
25
收起
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星