@Configuration @EnableTransactionManagement @PropertySource(value = {"classpath:config/source.properties"}) public class BeanConfig { @Autowired private Environment env; @Bean(destroyMethod = "close") public DataSource dataSource() { DruidDataSource dataSource = new DruidDataSource(); dataSource.setDriverClassName(env.getProperty("source.driverClassName").trim()); dataSource.setUrl(env.getProperty("source.url").trim()); dataSource.setUsername(env.getProperty("source.username").trim()); dataSource.setPassword(env.getProperty("source.password").trim()); return dataSource; } @Bean public JdbcTemplate jdbcTemplate() { JdbcTemplate jdbcTemplate = new JdbcTemplate(); jdbcTemplate.setDataSource(dataSource()); return jdbcTemplate; } }
src/main/resources/config/source.properties 中配置数据源信息