@Configuration
public class DatabaseConfig {
@Bean(name = "dsSlave")
@ConfigurationProperties(prefix="spring.mysql_slave")
public DataSource slaveDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "dsMaster")
@Primary
@ConfigurationProperties(prefix="spring.mysql_master")
public DataSource masterDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "jdbcSlave")
@Autowired
public JdbcTemplate slaveJdbcTemplate(@Qualifier("dsSlave") DataSource dsSlave) {
return new JdbcTemplate(dsSlave);
}
@Bean(name = "jdbcMaster")
@Autowired
public JdbcTemplate masterJdbcTemplate(@Qualifier("dsMaster")DataSource dsMaster) {
return new JdbcTemplate(dsMaster);
}
}
@Component
public class SqlService {
@Autowired
@Qualifier("jdbcSlave")
private JdbcTemplate jdbcTemplate;
public String getHelloMessage() {
String host = jdbcTemplate.queryForObject("select @@hostname;", String.class);
System.out.println(host);
return "Hello";
}
}
No comments:
Post a Comment