Configure DataSource programmatically in Spring Boot

You can use DataSourceBuilder if you are using jdbc starter. Also, in order to override the default autoconfiguration bean you need to mark your bean as a @Primary In my case I have properties starting with datasource.postgres prefix. E.g @ConfigurationProperties(prefix = “datasource.postgres”) @Bean @Primary public DataSource dataSource() { return DataSourceBuilder .create() .build(); } If it … Read more

How to use JNDI DataSource provided by Tomcat in Spring?

If using Spring’s XML schema based configuration, setup in the Spring context like this: <beans xmlns=”http://www.springframework.org/schema/beans” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns:jee=”http://www.springframework.org/schema/jee” xsi:schemaLocation=” http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd”> … <jee:jndi-lookup id=”dbDataSource” jndi-name=”jdbc/DatabaseName” expected-type=”javax.sql.DataSource” /> Alternatively, setup using simple bean configuration like this: <bean id=”DatabaseName” class=”org.springframework.jndi.JndiObjectFactoryBean”> <property name=”jndiName” value=”java:comp/env/jdbc/DatabaseName”/> </bean> You can declare the JNDI resource in tomcat’s server.xml using something … Read more

Spring Boot configure and use two data sources

Here you go. Add in your application.properties file: #first db spring.datasource.url = [url] spring.datasource.username = [username] spring.datasource.password = [password] spring.datasource.driverClassName = oracle.jdbc.OracleDriver #second db … spring.secondDatasource.url = [url] spring.secondDatasource.username = [username] spring.secondDatasource.password = [password] spring.secondDatasource.driverClassName = oracle.jdbc.OracleDriver Add in any class annotated with @Configuration the following methods: @Bean @Primary @ConfigurationProperties(prefix=”spring.datasource”) public DataSource primaryDataSource() { return … Read more