Monday, 15 February 2010

java - Adding a second datasource - SpringBoot RepositoryRestService PersistenceConfig -



java - Adding a second datasource - SpringBoot RepositoryRestService PersistenceConfig -

i'm trying find best approach adding sec datasource our application. main purpose expose crud ops against db via rest, & need bounce against 2nd db authentication , role management. not using xml configs.

is there way add together sec datasource bean in existing persistenceconfig.java file, or need replicate whole config class sec db instance?

the application:

package foo;

import foo.config.persistenceconfig; import foo.config.repositoryrestconfig; import foo.config.webconfig; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.enableautoconfiguration; import org.springframework.context.annotation.componentscan; import org.springframework.context.annotation.configuration; import org.springframework.context.annotation.import; import org.springframework.data.jpa.repository.config.enablejparepositories; @configuration @componentscan @enablejparepositories @import({persistenceconfig.class, webconfig.class, repositoryrestconfig.class}) @enableautoconfiguration public class application { public static void main(string[] args) { springapplication.run(application.class, args); } }

the repo:

package foo.repository; import foo.widget; import org.springframework.data.repository.crudrepository; import org.springframework.data.repository.query.param; import org.springframework.data.rest.core.annotation.repositoryrestresource; import java.util.list; @repositoryrestresource(collectionresourcerel = "widgets", path = "widgets") public interface widgetsrepository extends crudrepository<widget, long> { list<widget> findbywidgetid(@param("widgetid") long widgetid); }

the persistence config :

package foo.config; import org.apache.commons.dbcp2.basicdatasource; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.configuration; import org.springframework.context.annotation.import; import org.springframework.context.annotation.primary; import org.springframework.data.jpa.repository.config.enablejparepositories; import org.springframework.data.rest.webmvc.config.repositoryrestmvcconfiguration; import org.springframework.orm.jpa.jpadialect; import org.springframework.orm.jpa.jpatransactionmanager; import org.springframework.orm.jpa.localcontainerentitymanagerfactorybean; import org.springframework.orm.jpa.vendor.database; import org.springframework.orm.jpa.vendor.hibernatejpadialect; import org.springframework.orm.jpa.vendor.hibernatejpavendoradapter; import org.springframework.transaction.platformtransactionmanager; import org.springframework.transaction.annotation.enabletransactionmanagement; import javax.persistence.persistencecontext; import javax.sql.datasource; @configuration @import(repositoryrestmvcconfiguration.class) @enablejparepositories @enabletransactionmanagement public class persistenceconfig { @bean public localcontainerentitymanagerfactorybean entitymanagerfactory() { final hibernatejpavendoradapter vendoradapter = new hibernatejpavendoradapter(); vendoradapter.setdatabase(database.sql_server); vendoradapter.setshowsql(true); final localcontainerentitymanagerfactorybean mill = new localcontainerentitymanagerfactorybean(); factory.setjpavendoradapter(vendoradapter); factory.setpackagestoscan("foo.model"); factory.setdatasource(datasource()); homecoming factory; } @bean(destroymethod = "close") public datasource datasource() { basicdatasource datasource = new basicdatasource(); datasource.setdriverclassname("com.microsoft.sqlserver.jdbc.sqlserverdriver"); datasource.seturl("jdbc:sqlserver://127.0.0.1:1433;databasename=foodb"); datasource.setusername("sa"); datasource.setpassword("*******"); datasource.settestonborrow(true); datasource.settestonreturn(true); datasource.settestwhileidle(true); datasource.settimebetweenevictionrunsmillis(1800000l); datasource.setnumtestsperevictionrun(3); datasource.setminevictableidletimemillis(1800000l); datasource.setvalidationquery("select 1"); homecoming datasource; } @bean public jpadialect jpadialect() { homecoming new hibernatejpadialect(); } @bean public platformtransactionmanager transactionmanager() { jpatransactionmanager txmanager = new jpatransactionmanager(); txmanager.setentitymanagerfactory(entitymanagerfactory().getobject()); homecoming txmanager; } }

thank assistance...

take @ @qualifier annotation. annotation able define various beans of same type , assign them names. equivalent of id parameter in bean xml tag.

this relevant part of spring documentation.

java spring-boot spring-data-jpa

No comments:

Post a Comment