添加第二个数据源 - SpringBoot RepositoryRestService PersistenceConfig [英] Adding a second datasource - SpringBoot RepositoryRestService PersistenceConfig
问题描述
我正在尝试找到向应用程序添加第二个数据源的最佳方法。主要目的是通过rest,& ;;暴露CRUD操作对数据库。需要针对第二个数据库进行身份验证和角色管理。我们没有使用XML配置。
I'm trying to find the best approach to adding a second datasource to our application. The main purpose is to expose CRUD ops against the db via rest, & need to bounce against a 2nd db for authentication and role management. We are not using XML configs.
有没有办法简单地在现有的PersistenceConfig.java文件中添加第二个数据源bean,或者我们是否需要复制整个配置第二个数据库实例的类?
Is there a way to simply add a second datasource bean in the existing PersistenceConfig.java file, or do we need to replicate the whole config class for the second db instance?
应用程序:
package foo;
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);
}
}
回购:
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);
}
持久性配置:
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 factory = new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(vendorAdapter);
factory.setPackagesToScan("foo.model");
factory.setDataSource(dataSource());
return 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");
return dataSource;
}
@Bean
public JpaDialect jpaDialect() {
return new HibernateJpaDialect();
}
@Bean
public PlatformTransactionManager transactionManager() {
JpaTransactionManager txManager = new JpaTransactionManager();
txManager.setEntityManagerFactory(entityManagerFactory().getObject());
return txManager;
}
}
感谢您的帮助.. 。
推荐答案
首先,值得注意的是 PersistenceConfig $中的几乎所有配置c $ c>是多余的,因为Spring Boot会自动为您配置它。几乎是非默认的,你需要指定的是你的DataSource配置,例如SQLServer URL。
First of all, it's worth noting that almost all of the configuration in PersistenceConfig
is redundant as Spring Boot will automatically configure it for you. Pretty much the only thing that is non-default and you need to specify is your DataSource configuration, for example the SQLServer URL.
有一个文档中描述如何配置的部分两个DataSource使用 @Primary
和 application.properties
:
There's a section in the documentation that describes how to configure two DataSources using @Primary
and application.properties
:
创建多个数据源与创建第一个数据源的工作方式相同。如果您使用JDBC或JPA的默认自动配置(那么任何@Autowired注入都将使用它),您可能希望将其中一个标记为@Primary。
Creating more than one data source works the same as creating the first one. You might want to mark one of them as @Primary if you are using the default auto-configuration for JDBC or JPA (then that one will be picked up by any @Autowired injections)."
@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();
}
然后使用 application.properties
和 datasource.primary $ c $配置这两个DataSources c>和
datasource.secondary
前缀:
例如:
datasource.primary.jdbcUrl=jdbc:sqlserver://127.0.0.1:1433;databaseName=fooDB
datasource.primary.user=sa
datasource.primary.password=secret
datasource.primary.jdbcUrl=jdbc:sqlserver://127.0.0.1:1433;databaseName=barDB
datasource.primary.user=sa
datasource.primary.password=secret
这篇关于添加第二个数据源 - SpringBoot RepositoryRestService PersistenceConfig的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!