我有一个带有2个数据源的Spring-boot应用程序。
application.properties
spring.datasource.url= jdbc:postgresql://localhost:5432/data1
spring.datasource.username=postgres
spring.datasource.password=pass
spring.secondDatasource.url= jdbc:mysql://localhost:3306/data2
spring.secondDatasource.username=root
spring.secondDatasource.password=pass
spring.jpa.hibernate.ddl-auto=create-drop
DataBaseConfiguration.java
@Configuration
public class DatabaseConfiguration {
@Bean
@Primary
@ConfigurationProperties(prefix="spring.datasource")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties(prefix="spring.secondDatasource")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
}
MessageRepository.java
public interface MessageRepository extends CrudRepository<Message, Long>{
}
MessageController.java
@Controller
@RequestMapping("/messages")
public class MessageController {
@Autowired
private MessageRepository repositoryPostGreSQL;
@RequestMapping(value="", method = RequestMethod.GET)
public String listPosta(Model model){
model.addAttribute("messages", repositoryPostGreSQL.findAll());
return "messages/list";
}
}
工作正常,2个数据库已连接并正常工作。
我需要知道的是,当我@Autowire
一个Repository
时,如何指定应将DataSource
巫婆联系起来?