您好,登錄后才能下訂單哦!
由于項(xiàng)目需要,最近研究了一下基于spring Boot與Spring Data JPA的多數(shù)據(jù)源配置問題。以下是傳統(tǒng)的單數(shù)據(jù)源配置代碼。這里使用的是Spring的Annotation在代碼內(nèi)部直接配置的方式,沒有使用任何XML文件。
@Configuration @EnableJpaRepositories(basePackages = "org.lyndon.repository") @EnableTransactionManagement @PropertySource("classpath:application.properties") public class JpaConfig { private static final String DATABASE_DRIVER = "db.driver"; private static final String DATABASE_URL = "db.url"; private static final String DATABASE_USER = "db.user"; private static final String DATABASE_PASSWORD = "db.password"; private static final String PACKAGES_TO_SCAN = "packages.to.scan"; private static final String HIBERNATE_DIALECT = "hibernate.dialect"; private static final String HIBERNATE_SHOW_SQL = "hibernate.show.sql"; @Resource private Environment env; @Bean public DataSource dataSource() { DruidDataSource source = new DruidDataSource(); source.setDriverClassName(env.getRequiredProperty(DATABASE_DRIVER)); source.setUrl(env.getRequiredProperty(DATABASE_URL)); source.setUsername(env.getRequiredProperty(DATABASE_USER)); source.setPassword(env.getRequiredProperty(DATABASE_PASSWORD)); return source; } @Bean public LocalContainerEntityManagerFactoryBean entityManagerFactory() { LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean(); factory.setDataSource(dataSource()); factory.setPersistenceProviderClass(HibernatePersistenceProvider.class); factory.setPackagesToScan(env.getRequiredProperty(PACKAGES_TO_SCAN)); factory.setJpaProperties(hibernateProperties()); factory.afterPropertiesSet(); return factory; } @Bean public PlatformTransactionManager transactionManager() { JpaTransactionManager manager = new JpaTransactionManager(); manager.setEntityManagerFactory(entityManagerFactory().getObject()); return manager; } @Bean public HibernateExceptionTranslator hibernateExceptionTranslator() { return new HibernateExceptionTranslator(); } private Properties hibernateProperties() { Properties properties = new Properties(); properties.put(HIBERNATE_DIALECT, env.getRequiredProperty(HIBERNATE_DIALECT)); properties.put(HIBERNATE_SHOW_SQL, env.getRequiredProperty(HIBERNATE_SHOW_SQL)); return properties; } }
但是這一配置是不能簡(jiǎn)單地?cái)U(kuò)展到多數(shù)據(jù)源配置的,因?yàn)镾pring Boot默認(rèn)會(huì)為開發(fā)人員做很多工作,而這些工作與多數(shù)據(jù)源的配置相沖突,因此需要修改原來的配置內(nèi)容。網(wǎng)上有很多講解多數(shù)據(jù)源配置的文章,但是這些文章大多使用的是XML配置的方式,而且沒有使用Spring Boot等比較新的Spring框架技術(shù)(比如很多人使用實(shí)現(xiàn)AbstractRoutingDataSource這一Spring提供的抽象類的方式,還需要切面的支持,無疑是相當(dāng)繁瑣的),已經(jīng)不適用于在最新的工程項(xiàng)目中使用了,因?yàn)樽钚碌腟pring框架可以為我們完成很多事情,我們只需要去適應(yīng)新的方法即可。為此,我通過研究Spring的官方文檔和不斷調(diào)試,實(shí)現(xiàn)了新的多數(shù)據(jù)源配置的方法,在此貼出,僅供參考。
首先,Spring的JPA是直接支持多數(shù)據(jù)源配置的,因此我們可以在配置文件或者代碼中直接配置多個(gè)數(shù)據(jù)源。由于多數(shù)據(jù)源配置可能會(huì)共享一些配置信息,因此使用繼承體系實(shí)現(xiàn)這種配置是最合適的。在例子中,我的ORM框架使用的是hibernate,而與Hibernate相關(guān)的配置信息我都放在了配置基類BaseJpaConfg中,代碼如下。
@Configuration @EnableTransactionManagement @PropertySource("classpath:application.properties") public class BaseJpaConfig { private static final String HIBERNATE_DIALECT = "hibernate.dialect"; private static final String HIBERNATE_SHOW_SQL = "hibernate.show.sql"; @Resource protected Environment env; @Bean public HibernateExceptionTranslator hibernateExceptionTranslator() { return new HibernateExceptionTranslator(); } protected Properties hibernateProperties() { Properties properties = new Properties(); properties.put(HIBERNATE_DIALECT, env.getRequiredProperty(HIBERNATE_DIALECT)); properties.put(HIBERNATE_SHOW_SQL, env.getRequiredProperty(HIBERNATE_SHOW_SQL)); return properties; } }
該類提供Hibernate相關(guān)信息。它有兩個(gè)子類,分別是SpringJpaConfig以及MysqlJpaConfig,分別配置了兩個(gè)數(shù)據(jù)源,連接到兩個(gè)不同的數(shù)據(jù)庫Schema,代碼如下。
@Configuration @EnableJpaRepositories(basePackages = {"org.lyndon.repository1"}, entityManagerFactoryRef = "springEntityManagerFactory", transactionManagerRef = "springTransactionManager") public class SpringJpaConfig extends BaseJpaConfig { private static final String DATABASE_DRIVER = "db.driver"; private static final String DATABASE_URL = "db.url"; private static final String DATABASE_USER = "db.user"; private static final String DATABASE_PASSWORD = "db.password"; private static final String PACKAGES_TO_SCAN = "packages.to.scan"; @Bean @ConfigurationProperties(prefix = "datasource.primary") public DataSource dataSource() { DruidDataSource source = new DruidDataSource(); source.setDriverClassName(env.getRequiredProperty(DATABASE_DRIVER)); source.setUrl(env.getRequiredProperty(DATABASE_URL)); source.setUsername(env.getRequiredProperty(DATABASE_USER)); source.setPassword(env.getRequiredProperty(DATABASE_PASSWORD)); return source; } @Bean(name = "springEntityManagerFactory") public LocalContainerEntityManagerFactoryBean springEntityManagerFactory() { LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean(); factory.setDataSource(dataSource()); factory.setPersistenceProviderClass(HibernatePersistenceProvider.class); factory.setPackagesToScan(env.getRequiredProperty(PACKAGES_TO_SCAN).split(",")); factory.setJpaProperties(hibernateProperties()); factory.afterPropertiesSet(); return factory; } @Bean(name = "springTransactionManager") @Primary public PlatformTransactionManager transactionManager() { JpaTransactionManager manager = new JpaTransactionManager(); manager.setEntityManagerFactory(springEntityManagerFactory().getObject()); return manager; } }
@Configuration @EnableJpaRepositories(basePackages = {"org.lyndon.repository2"}, entityManagerFactoryRef = "mysqlEntityManagerFactory", transactionManagerRef = "mysqlTransactionManager") public class MysqlJpaConfig extends BaseJpaConfig { private static final String DATABASE_DRIVER = "db.driver"; private static final String DATABASE_URL = "db.url2"; private static final String DATABASE_USER = "db.user"; private static final String DATABASE_PASSWORD = "db.password"; private static final String PACKAGES_TO_SCAN = "packages.to.scan2"; @Bean @ConfigurationProperties(prefix = "datasource.secondary") public DataSource dataSource2() { DruidDataSource source = new DruidDataSource(); source.setDriverClassName(env.getRequiredProperty(DATABASE_DRIVER)); source.setUrl(env.getRequiredProperty(DATABASE_URL)); source.setUsername(env.getRequiredProperty(DATABASE_USER)); source.setPassword(env.getRequiredProperty(DATABASE_PASSWORD)); return source; } @Bean(name = "mysqlEntityManagerFactory") public LocalContainerEntityManagerFactoryBean mysqlEntityManagerFactory() { LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean(); factory.setDataSource(dataSource2()); factory.setPersistenceProviderClass(HibernatePersistenceProvider.class); factory.setPackagesToScan(env.getRequiredProperty(PACKAGES_TO_SCAN).split(",")); factory.setJpaProperties(hibernateProperties()); factory.afterPropertiesSet(); return factory; } @Bean(name = "mysqlTransactionManager") public PlatformTransactionManager transactionManager() { JpaTransactionManager manager = new JpaTransactionManager(); manager.setEntityManagerFactory(mysqlEntityManagerFactory().getObject()); return manager; } }
兩份配置代碼使用了各自的數(shù)據(jù)源、實(shí)體管理對(duì)象以及事務(wù)管理對(duì)象。這里要注意的有兩點(diǎn),首先是實(shí)體管理對(duì)象。Spring Boot框架為方便開發(fā)者,默認(rèn)會(huì)尋找名為“entityManagerFactory”的Bean作為實(shí)體管理的實(shí)現(xiàn),但是我們這里使用了兩個(gè)實(shí)體管理對(duì)象,名稱也并不是默認(rèn)的“entityManagerFactory”,因此必須在各自的EnableJpaRepositories的Annotation中指明使用的實(shí)體管理對(duì)象。第二點(diǎn),就是事務(wù)管理對(duì)象。Spring Boot會(huì)提供一個(gè)默認(rèn)的事務(wù)管理對(duì)象的實(shí)現(xiàn),但是我們?cè)谶@里使用了兩個(gè)不同的事務(wù)管理對(duì)象,因此我們也需要在各自的EnableJpaRepositories的Annotation中指明使用的事務(wù)管理對(duì)象。除此以外,我們還必須指定其中一個(gè)事務(wù)管理對(duì)象為主要對(duì)象(使用Primary這一Annotation),讓Spring能有主次地使用相應(yīng)的事務(wù)管理對(duì)象。
以上就是本文的主要內(nèi)容。關(guān)于Service的配置等問題,由于和單數(shù)據(jù)源時(shí)是一樣的,因此就不再贅述了?;谏鲜龃a,我們就可以使用Spring Boot實(shí)現(xiàn)多數(shù)據(jù)源之間的無縫切換了,是不是很輕松?如果你不想使用代碼配置的方式,也可以使用XML文件代替,配置的核心屬性與上文相同,可以自己加以琢磨。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。