java - Spring如何为环境找到bean?
问题描述
我有这个正确的代码。而且我不明白 spring 如何为 Environment 接口找到 bean。帮我。谢谢
@Configuration
@ComponentScan(value = "ru.itis")
@PropertySource("application.properties")
public class AppConfig {
@Autowired
private Environment environment;
@Bean
public NamedParameterJdbcTemplate template() {
return new NamedParameterJdbcTemplate(dataSource());
}
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(environment.getProperty("jdbc.driver"));
dataSource.setUrl(environment.getProperty("jdbc.url"));
dataSource.setUsername(environment.getProperty("jdbc.username"));
dataSource.setPassword(environment.getProperty("jdbc.password"));
return dataSource;
}
}
解决方案
推荐阅读
- google-apps-script - 有没有办法将特定工作表从一个工作簿复制到特定(和不同)文件夹作为谷歌中自己的独立工作表?
- flutter - 架构问题 - 当您可以更换组件时,导航的意义何在?
- python - 是否可以减小 tkinter 中的按钮大小?
- mysql - 从表中获取实体并对字段求和 - Sequelize
- python - 如何使用列表作为绘图表的值?
- java - java.lang.ArrayIndexOutOfBoundsException:索引 0 超出长度 0 的范围
- python - 用于控制 Python 脚本是否仍在运行(未冻结)的 Shell 脚本
- angular - Angular Material 使用 sidenav 和工具栏作为可重用组件
- django - Django ForeignKey on_delete = SET_NULL 不起作用
- python - 无法使用 For 循环将数据框的列名附加到列表中