غالبًا ما نواجه مشاكل مع مصادر بيانات متعددة في المشاريع ، وخاصة مشاريع مثل مزامنة البيانات أو مهام التوقيت. إن أكثر شيء مزعج حول مصادر البيانات المتعددة ليس تكوين مصادر بيانات متعددة ، ولكن كيفية تبديل مصادر البيانات بطريقة مرنة وديناميكية. على سبيل المثال ، في مشروع إطار عمل الربيع وإطار السبات ، غالبًا ما نقوم بتكوين بيانات بيانات في تكوين الربيع للاتصال بقاعدة البيانات ، ثم ربطها بجلسة SessionFactory ، ثم حدد SessionFactory في رمز طبقة DAO لتنفيذ عمليات قاعدة البيانات.
كما هو موضح في الشكل أعلاه ، لا بد أن تكون كل كتلة ملزمة. إذا كانت مصادر بيانات متعددة ، يمكن أن تكون هي نفسها كما في الشكل أدناه.
يمكن ملاحظة أنه تم كتابة اثنين من SessionFactory في رمز الطبقة DAO. وبهذه الطريقة ، إذا كان هناك مصدر بيانات آخر في المستقبل ، فيجب عليك تغيير الرمز وإضافة SessionFactory. من الواضح أن هذا لا يتوافق مع مبدأ الفتح والإغلاق.
ثم يجب أن تكون الطريقة الصحيحة للقيام بذلك
الرمز كما يلي:
1. ApplicationContext.xml
<؟ xmlns: aop = "http://www.springframework.org/schema/aop" xmlns: jdbc = "http://www.springframework.org/schema/jdbc" xmlns: jee = "http://www.springframework.org/schema/jee" Xmlns: jms = "http://www.springframework.org/schema/jms" xmlns: lang = "http://www.springframework.org/schema/lang" xmlns: mvc = "http://www.springframework.org/schema/mvc" Xmlns: Task = "http://www.springframework.org/schema/task" xmlns: tx = "http://www.springframework.org/schema/tx XSI: Schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schame/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd http://www.springframework.org/schema/JMS http://www.springframework.org/schema/jms/spring-jms-3.1.xsd http://www.springframework.org/schema/Lang http://www.springframework.org/schema/mvc http://www.springframework.org/schema/oxm/spring-oxm-3.1.xsd http://www.springframework.org/schema/oxm/spring-oxm-3.1.xsd http://www.springframewok http://www.springframework.org/schema/task/spring-task-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/txm http://www.springframework.org/schema/Util <property name = "socations"> <list> <value> classpath: com/resource/config.properties </value> </list> </sprement> </boun> <bean id = "datasourceone" destroy-method = "close"> <property name = "driverClass" value = "$ {dbone.jdbc.dbc.driverclass} value = "$ {dbone.jdbc.url}" /> <property name = "user" value = "$ {dbone.jdbc.user}" /> <property name = "password" value = "$ {dbone.jdbc.password}" /> <property name = "initialpoolsize" value = {dbone. name = "minpoolsize" value = "$ {dbone.jdbc.minpoolsize}" /> <property name = "maxpoolsize" value = "$ {dbone.jdbc.maxpoolsize}" /> </bean id = "datasourcetwo" value = "$ {dbtwo.jdbc.driverclass}" /> <property name = "jdbcurl" value = "$ {dbtwo.jdbc.url}" /> <property name = "user" value = "$ {dbtwo.jdbc.user}" /> name = "initialPoolSize" value = "$ {dbtwo.jdbc.initialPoolsize}" /> <property name = "minpoolsize" value = "$ {dbtwo.jdbc.minpoolsize}" /> <property name = "maxpoolsize ID = "DynamicDataSource"> <property name = "TargetDataSources"> <map-type-type = "java.lang.String"> <intervation value-ref = "datasourceone" key = "dataSourceone> </entrate> <interpt name = "defaultTargetDataSource" ref = "dataSourceone"> </property> </bean> <bean id = "sessionfactory"> <property name = "dataSource" ref = "dynamicDataSource"/> <property name = "hibernateProperties"> <props> <props> key = "hibernate.dialect"> org.hibernate.dialect.mysqldialect </prop> <prop key = "hibernate.current_session_context_class"> org.springframework.orm.hibernate4 key = "hibernate.format_sql"> true </prop> <prop key = "hbm2ddl.auto"> إنشاء </props> </props> </property> <property name = "packagestoscan"> <list> <value> com.po </value> </rip ref = "sessionfactory" /> </bean> <aop: config> <aop: pointcut id = "TransactionPointCut" expression = "execution (*com.dao ..*. المعاملات-manager = "TransactionManager"> <tx: entributes> <tx: method name = "add*" spection = "required" /> <tx: method name = "save*" spection = "required" /> <tx: method name = "update* /> < /tx: سمات> < /tx: نصيحة> <aOP: config> <aop: side id = "dataSourCeaspect" ref = "dataSourceIntercept"> <aop: pointcut id = "daOone" expression = "execution (*com.dao.one.*. com.dao.two.*.*. 2. DynamicDataSource.Class
حزمة com.core ؛ استيراد org.springframework.jdbc.datasource.lookup.abstractroutingdataSource ؛ يمتد DynamicDataSource العام العام abstractroutingDataSource {Override كائن محمي DETERNECURRENTOKTOFKUPKEY () {return databaseContextholder.getCustomerType () ؛ }} 3. databaseContextholder.class
حزمة com.core ؛ databaseContextholder {private static final finalocal <string> contexTholder = new threadlocal <string> () ؛ public static void setCustomerType (String customerType) {contextholder.set (customerType) ؛ } السلسلة الثابتة العامة getCustomerType () {return contextholder.get () ؛ } public static void clearCustomerType () {contextholder.remove () ؛ }} 4. DataSourceInterceptor.Class
حزمة com.core ؛ استيراد org.aspectj.lang.joinpoint ؛ استيراد org.springframework.stereotype.component ؛ component public class dataSourceInterceptor {public void setDataSourceone (JoinPoint jp) {databaseContextholder.setCustomerType ("datasourceone") ؛ } public void setDataSourcetwo (JoinPoint jp) {databaseContextholder.setCustomerType ("datasourcetwo") ؛ }} 5. فئة كيان بو
حزمة com.po ؛ استيراد javax.persistence.column ؛ استيراد javax.persistence.entity ؛ استيراد javax.persistence.id ؛ استيراد javax.persistence.table ؛ entity @table (name = "btsf_brand" ، schema = "hotel") class public class {private string id ؛ أسماء السلسلة الخاصة ؛ عنوان URL الخاص بالسلسلة ؛ @column (name = "id" ، فريدة = true ، nullable = false ، length = 10) public string getId () {return this.id ؛ } public void setId (string id) {this.id = id ؛ } column (name = "names" ، nullable = false ، length = 50) public string getNames () {return this.names ؛ } public void setNames (أسماء السلسلة) {this.names = names ؛ } column (name = "url" ، length = 200) public string geturl () {return this.url ؛ } public void seturl (url url) {this.url = url ؛ }} حزمة com.po ؛ استيراد javax.persistence.column ؛ استيراد javax.persistence.entity ؛ استيراد javax.persistence.id ؛ استيراد javax.persistence.table ؛ entity @table (name = "city" ، schema = "car") Class Clist City {private Integer id ؛ اسم السلسلة الخاصة ؛ @column (name = "id" ، فريد = صحيح ، لا ، nullable = false) عدد صحيح عام getId () {معرف الإرجاع ؛ } public void setId (integer id) {this.id = id ؛ } column (name = "names" ، nullable = false ، length = 50) public string getName () {return name ؛ } public void setName (اسم السلسلة) {this.name = name ؛ }} 6. Branddaoimpl.Class
حزمة com.dao.one ؛ استيراد java.util.list ؛ استيراد javax.annotation.Resource ؛ استيراد org.hibernate.query ؛ استيراد org.hibernate.SessionFactory ؛ استيراد org.springframework.stereotePe.Repository ؛ استيراد com.po.brand ؛ repository الفئة العامة branddaoimpl تنفذ ibranddao {resource sessionfactory sessionfactory ؛ suppressWarnings ("Unchected") Override Public List <brand> findall () {String hql = "from brand" ؛ Query Query = SessionFactory.getCurrentsession (). CreateEquery (HQL) ؛ return query.list () ؛ }} 7. Citydaoimpl.Class
حزمة com.dao.two ؛ استيراد java.util.list ؛ استيراد javax.annotation.Resource ؛ استيراد org.hibernate.query ؛ استيراد org.hibernate.SessionFactory ؛ استيراد org.springframework.stereotePe.Repository ؛ استيراد com.po.city ؛ repository public class citydaoimpl تنفذ icitydao {resource sessionfactory sessionfactory ؛ suppressWarnings ("Unchected") Override القائمة العامة <City> Find () {String Hql = "From City" ؛ Query Query = SessionFactory.getCurrentsession (). CreateEquery (HQL) ؛ return query.list () ؛ }} 8. daotest.class
حزمة com.test ؛ استيراد java.util.list ؛ استيراد javax.annotation.Resource ؛ استيراد org.junit.test ؛ استيراد org.junit.runner.runwith ؛ استيراد org.springframework.test.context.contextConfiguration ؛ استيراد org.springframework.test.context.junit4.SpringJunit4ClassRunner ؛ استيراد org.springframework.test.context.transaction.transactionConfiguration ؛ استيراد com.dao.one.ibranddao ؛ استيراد com.dao.two.icitydao ؛ استيراد com.po.brand ؛ استيراد com.po.city ؛ Runwith (springJunit4ClassRunner.Class) contextConfiguration (مواقع = "classpath: com/resource/applicationContext.xml") transactionConfiguration (TransactactionManager = "TransactionManager" ، defaultrollback = false) public daotest {rresource ibranddao branddao ؛ Resource Private Icitydao Citydao ؛ Test public void testlist () {list <brand> brands = branddao.findall () ؛ System.out.println (brands.size ()) ؛ قائمة <City> Cities = CityDao.find () ؛ system.out.println (cities.size ()) ؛ }} استخدم AOP لتحقيق الغرض من تغيير مصدر البيانات ديناميكيًا. عندما نحتاج إلى إضافة مصادر البيانات ، نحتاج فقط إلى إضافة تكوين AOP في ملف تكوين ApplicationContext وإنشاء مستقبلات بيانات جديدة. دون تغيير أي رمز.
ما سبق هو كل محتوى هذه المقالة. آمل أن يكون ذلك مفيدًا لتعلم الجميع وآمل أن يدعم الجميع wulin.com أكثر.