spring_reference/IX. ‘How-to’ guides/67.2. Configure Two DataSou...

18 lines
596 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

### 67.2. 配置两个数据源
创建多个数据源和创建第一个工作都是一样的。如果使用针对JDBC或JPA的默认自动配置你可能想要将其中一个设置为`@Primary`(然后它就能被任何`@Autowired`注入获取)。
```java
@Bean
@Primary
@ConfigurationProperties(prefix="datasource.primary")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties(prefix="datasource.secondary")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
```