เรามักจะพบปัญหาเกี่ยวกับแหล่งข้อมูลหลายแหล่งในโครงการโดยเฉพาะโครงการเช่นการซิงโครไนซ์ข้อมูลหรืองานเวลา สิ่งที่ลำบากที่สุดเกี่ยวกับแหล่งข้อมูลหลายแหล่งไม่ได้เป็นการกำหนดค่าแหล่งข้อมูลหลายแหล่ง แต่วิธีเปลี่ยนแหล่งข้อมูลในลักษณะที่ยืดหยุ่นและมีชีวิตชีวา ตัวอย่างเช่นในโครงการ Framework Spring และ Hibernate เรามักจะกำหนดค่าแหล่งข้อมูลในการกำหนดค่าฤดูใบไม้ผลิเพื่อเชื่อมต่อกับฐานข้อมูลจากนั้นผูกไว้กับ SessionFactory จากนั้นระบุ SessionFactory ในรหัสเลเยอร์ DAO เพื่อดำเนินการฐานข้อมูล
ดังที่แสดงในรูปด้านบนแต่ละบล็อกจะถูกผูกไว้ หากเป็นแหล่งข้อมูลหลายแหล่งมันจะเหมือนกับในรูปด้านล่างเท่านั้น
จะเห็นได้ว่า SessionFactory สองตัวถูกเขียนในรหัสเลเยอร์ DAO ด้วยวิธีนี้หากมีแหล่งข้อมูลอื่นในอนาคตคุณต้องเปลี่ยนรหัสและเพิ่ม SessionFactory เห็นได้ชัดว่าสิ่งนี้ไม่สอดคล้องกับหลักการของการเปิดและปิด
จากนั้นวิธีที่ถูกต้องในการทำควรจะเป็น
รหัสมีดังนี้:
1. ApplicationContext.xml
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://ww.w3.org/2001/xml xmlns: aop = "http://www.springframework.org/schema/aop" xmlns: cache = "http://www.springframework.org/schema/cache" xmlns: บริบท = "http:/ 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: oxm = "http://www.springframework.org/schema/oxm" xmlns: p = "http:/ http:/ xmlns: task = "http://www.springframework.org/schema/task" xmlns: tx = "http://www.springframework.org/schema/tx" xmlns: util = "http:/ XSI: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/cache http://ww.springframework 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/schemabd-schemabc http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3 http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.1.xsd http://ww.springframework.org/schema/schema http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.1.xsd http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/oxm http://ww.springframework http://www.springframework.org/schema/oxm/spring-oxm-3.1.xsd http://www.springframework.org/schema/task http://ww.springframework http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://ww.springframework http://www.springframework.org/schema/util/spring-util-3.1.xsd "> <บริบท: Annotation-config/> <บริบท: Component-Scan base-package =" com "> <value> classpath: com/resource/config.properties </value> </list> </property> </ebean> <bean id = "dataSourceone" destroy-method = "close"> <property name = "driverclass" value = "$ {dbone.jdbc.driverclass}"/> < /> <property name = "user" value = "$ {dbone.jdbc.user}" /> <property name = "รหัสผ่าน" value = "$ {dbone.jdbc.password}" /> <property name = "InitialPoolSize" value = "$ {dbone.jdbc.initialsize value = "$ {dbone.jdbc.minpoolsize}" /> <property name = "maxPoolSize" value = "$ {dbone.jdbc.maxpoolsize}" /> </ebean> <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" <bean id = "dynamicDataSource"> <property name = "targetDataSources"> <แผนที่ key-type = "java.lang.string"> <ค่ารายการ ref = "dataSourceOne" key = "dataSourceOne"> </entry> name = "defaultTargetDataSource" ref = "dataSourceOne"> </คุณสมบัติ> </ebean> <bean id = "sessionFactory"> <property name = "dataSource" ref = "dynamicDataSource"/> <property name = "HiberNateProperties" key = "hibernate.dialect"> org.hibernate.dialect.mysqldialect </prop> <prop key = "hibernate.current_session_context_class"> org.springframework.orm.hibernate4.springsessionContext key = "hibernate.format_sql"> true </prop> <prop key = "hbm2ddl.auto"> สร้าง </prop> </props> </คุณสมบัติ> <property name = "Packagestoscan"> <list> ref = "sessionfactory" /> </ebean> <aop: config> <aop: pointcut id = "transactionpointcut" expression = "การดำเนินการ (*com.dao ..*.*(.. ))" /> <aop: คำแนะนำที่ปรึกษา-ref = "txadvice" Transaction-Manager = "TransactionManager"> <tx: คุณลักษณะ> <tx: method name = "เพิ่ม*" การแพร่กระจาย = "จำเป็น" /> <tx: method name = "บันทึก*" การแพร่กระจาย = "จำเป็น" /> <tx: method name = "update*" การเผยแพร่ = "จำเป็น" /> rest-only = "true" /> < /tx: attributes> < /tx: คำแนะนำ> <aop: config> <aop: ide id = "dataSourceaspect" ref = "dataSourceIntercept"> <aop: pointcut id = "daoone" expression = "com.dao.one.* Expression = "Execution (*com.dao.two.*.*(.. ))" /> <aop: ก่อน pointcut-ref = "daoone" method = "setDataSourceOne" /> <aop: ก่อน pointcut-ref = "daotwo" method = 2. DynamicDataSource.class
แพ็คเกจ com.core; นำเข้า org.springframework.jdbc.datasource.lookup.abstractroutingDatasource; Public Class DynamicDataSource ขยาย AbsTractroutingDataSource {@Override วัตถุที่ได้รับการป้องกันการกำหนดค่าความน่าสนใจ () {ส่งคืน DatabasEcontextholder.getCustomerType (); - 3. databasecontextholder.class
แพ็คเกจ com.core; คลาสสาธารณะ DatabasEcontextholder {ส่วนตัวคงที่ threadLocal <String> contextholder = ใหม่ ThreadLocal <String> (); โมฆะคงที่สาธารณะ setCustomerType (String customerType) {contextholder.set (CustomerType); } สตริงคงที่สาธารณะ getCustomerType () {return contextholder.get (); } โมฆะสาธารณะคงที่ ClearCustomerType () {contextholder.remove (); - 4. DataSourceInterceptor.class
แพ็คเกจ com.core; นำเข้า org.aspectj.lang.joinpoint; นำเข้า org.springframework.stereotype.component; @Component คลาสสาธารณะ DataSourceInterceptor {โมฆะสาธารณะ setDataSourceOne (JoinPoint JP) {Databasecontextholder.setCustomerType ("DataSourceOne"); } โมฆะสาธารณะ setDataSourCetWo (JoinPoint JP) {DatabasEcontextholder.setCustomerType ("DataSourCetWo"); - 5. ชั้นเอนทิตี PO
แพ็คเกจ com.po; นำเข้า Javax.persistence.column; นำเข้า Javax.persistence.entity; นำเข้า Javax.persistence.id; นำเข้า Javax.persistence.Table; @ENTITY @Table (name = "btsf_brand", schema = "โรงแรม") แบรนด์ระดับสาธารณะ {รหัสสตริงส่วนตัว; ชื่อสตริงส่วนตัว; URL สตริงส่วนตัว @ID @Column (name = "id", ไม่ซ้ำกัน = true, nullable = false, length = 10) Public String getId () {return this.id; } โมฆะสาธารณะ setId (รหัสสตริง) {this.id = id; } @column (name = "ชื่อ", nullable = false, length = 50) สตริงสาธารณะ getNames () {return this.names; } โมฆะสาธารณะ setNames (ชื่อสตริง) {this.names = ชื่อ; } @column (name = "url", length = 200) สตริงสาธารณะ getUrl () {return this.url; } โมฆะสาธารณะ setUrl (rl String) {this.url = url; - แพ็คเกจ com.po; นำเข้า Javax.persistence.column; นำเข้า Javax.persistence.entity; นำเข้า Javax.persistence.id; นำเข้า Javax.persistence.Table; @ENTITY @Table (name = "City", schema = "Car") เมืองชั้นเรียนสาธารณะ {ID จำนวนเต็มส่วนตัว; ชื่อสตริงส่วนตัว; @id @column (name = "id", imple = true, nullable = false) จำนวนเต็มสาธารณะ getId () {return id; } โมฆะสาธารณะ setId (ID จำนวนเต็ม) {this.id = id; } @column (name = "ชื่อ", nullable = false, length = 50) สตริงสาธารณะ getName () {return name; } โมฆะสาธารณะ setName (ชื่อสตริง) {this.name = name; - 6. Branddaoimpl.class
แพ็คเกจ com.dao.one; นำเข้า java.util.list; นำเข้า Javax.annotation.Resource; นำเข้า org.hibernate.query; นำเข้า org.hibernate.sessionfactory; นำเข้า org.springframework.stereotype.repository; นำเข้า com.po.brand; @Repository Class Public Branddaoimpl ใช้ ibranddao {@Resource Protected SessionFactory SessionFactory; @suppresswarnings ("ไม่ได้ตรวจสอบ") @Override List Public <brand> findall () {String hql = "จาก Brand"; Query Query = SessionFactory.getCurrentsession (). CreateQuery (HQL); return query.list (); - 7. CityDaoimpl.Class
แพ็คเกจ com.dao.two; นำเข้า java.util.list; นำเข้า Javax.annotation.Resource; นำเข้า org.hibernate.query; นำเข้า org.hibernate.sessionfactory; นำเข้า org.springframework.stereotype.repository; นำเข้า com.po.city; @Repository Class Public CityDaoimpl ใช้ IcityDao {@Resource Private SessionFactory SessionFactory; @suppresswarnings ("ไม่ได้ตรวจสอบ") @Override List Public <ity> ค้นหา () {String hql = "จากเมือง"; Query Query = SessionFactory.getCurrentsession (). CreateQuery (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 (locations = "classpath: com/resource/applicationcontext.xml") @transactionConfiguration (transactionManager = "transactionManager", defaultrollback = false) @Resource Private Icitydao Citydao; @Test Public Void TestList () {รายการ <brand> Brands = BrandDao.Findall (); System.out.println (Brands.size ()); รายการ <ity> เมือง = citydao.find (); System.out.println (Cities.size ()); - ใช้ AOP เพื่อให้บรรลุวัตถุประสงค์ของการเปลี่ยนแหล่งข้อมูลแบบไดนามิก เมื่อเราต้องการเพิ่มแหล่งข้อมูลเราจำเป็นต้องเพิ่มการกำหนดค่า AOP ในไฟล์การกำหนดค่า ApplicationContext และสร้าง DataSourceInterceptor ใหม่ โดยไม่ต้องเปลี่ยนรหัสใด ๆ
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น