2

I have a Spring-boot application with 2 Datasources.

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"; } } 

It is working fine, the 2 Databases are connected and working.

What I need to know is that when I @Autowire a Repository how do I specify witch DataSource should be linked?

3
  • What specifically are you referring to with Repository? Commented May 16, 2016 at 18:09
  • @SotiriosDelimanolis I added the code Commented May 16, 2016 at 18:12
  • Also, see this. Commented May 16, 2016 at 18:14

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.