[OSGi] Eclipse RCP环境中定义MyBatis与Spring的整合之DAO与申明式事务
2012-09-11 10:23
465 查看
OSGi程序的开发与基于Java的N-Tier的程序的开发有着千丝万缕的联系,过去,我们人为的将程序从结构上进行划分,这样职责分明,也符合面向对象设计的高内聚低耦合的思想。而OSGi本身就是基于组件的开发,各个组件之间的依赖通过Import-Package和Export-Package来实现,而且通过组件间的申明式Service(服务)完成组件之间的协作,OSGi与生俱来就是基于接口开发的高内聚且松散耦合的,与Spring可以说天生就是一对~
最近有一个小小的项目使用到了RCP,MyBatis和Spring Dynamic Module,首先,整个程序中有关的组件依赖如下图所示:
注:所有Bundle都会依赖Domain Bundle
DAO Bundle:定义了MyBatis中Mapper Interface,只有mapper接口的定义,例如CustomerMapper
DAOImpl Bundle:定义了映射XML文件,Spring的Bean Definition中包含了数据库连接,Mapper Bean和事务管理器的申明
Service Bundle:定义了服务接口,例如BusinessService定义了addCustomer, updateCustomer, deleteCustomer, getCustomer等业务方法
ServiceImpl Bundle:服务的实现,Spring的Bean Definition中包含了服务的申明和Export。BusinessServiceImpl会使用CustomerMapper来实现业务逻辑。
1. DAOImpl Bundle的Application Context例子:
2. DAOImpl Bundle必须将Mapper和TransactionManager Export为Bundle Service,那么在ServiceImpl中才能使用Mapper访问数据库,事务也才能定义在ServiceImpl的方法调用之上。
3. ServiceImpl Bundle会引用DaoImpl Bundle中定义的transaction Manager和customer mapper(通过osgi:reference标签和OsgiServiceProxyFactoryBean来实现)
4. ServiceImpl Bundle中Service的定义和Spring申明式事务
5. 同样,也可以在ServiceImple中将businessService Export为bundle service供其他bundle引用
最近有一个小小的项目使用到了RCP,MyBatis和Spring Dynamic Module,首先,整个程序中有关的组件依赖如下图所示:
注:所有Bundle都会依赖Domain Bundle
DAO Bundle:定义了MyBatis中Mapper Interface,只有mapper接口的定义,例如CustomerMapper
DAOImpl Bundle:定义了映射XML文件,Spring的Bean Definition中包含了数据库连接,Mapper Bean和事务管理器的申明
Service Bundle:定义了服务接口,例如BusinessService定义了addCustomer, updateCustomer, deleteCustomer, getCustomer等业务方法
ServiceImpl Bundle:服务的实现,Spring的Bean Definition中包含了服务的申明和Export。BusinessServiceImpl会使用CustomerMapper来实现业务逻辑。
1. DAOImpl Bundle的Application Context例子:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- Property Place Holder Configuration --> <context:property-placeholder location="classpath:configuration.properties"/> <!-- Data Source Configuration --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="initialSize" value="${jdbc.initialSize}" /> <property name="maxActive" value="${jdbc.maxActive}" /> <property name="maxWait" value="${jdbc.maxWait}" /> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- SQL Session Factory Configuration --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:fugaobatis.xml" /> </bean> <!-- Mapper Bean --> <bean id="customerMapper" scope="bundle" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="org.sean.fugao.data.persistence.CustomerMapper"/> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> </bean> </beans>
2. DAOImpl Bundle必须将Mapper和TransactionManager Export为Bundle Service,那么在ServiceImpl中才能使用Mapper访问数据库,事务也才能定义在ServiceImpl的方法调用之上。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:osgi="http://www.springframework.org/schema/osgi" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd"> <osgi:service ref="transactionManager" interface="org.springframework.transaction.PlatformTransactionManager" /> <bean id="customerMapperExporter" class="org.springframework.osgi.service.exporter.support.OsgiServiceFactoryBean"> <property name="target"> <ref bean="customerMapper"/> </property> <property name="interfaces"> <list> <value>org.sean.fugao.data.persistence.CustomerMapper</value> </list> </property> </bean> </beans>
3. ServiceImpl Bundle会引用DaoImpl Bundle中定义的transaction Manager和customer mapper(通过osgi:reference标签和OsgiServiceProxyFactoryBean来实现)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:osgi="http://www.springframework.org/schema/osgi" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd"> <osgi:reference id="transactionManager" interface="org.springframework.transaction.PlatformTransactionManager"/> <bean id="customerMapperService" class="org.springframework.osgi.service.importer.support.OsgiServiceProxyFactoryBean"> <property name="beanName" value="customerMapper"/> <property name="interfaces"> <list> <value>org.sean.fugao.data.persistence.CustomerMapper</value> </list> </property> </bean> </beans>
4. ServiceImpl Bundle中Service的定义和Spring申明式事务
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <bean id="abstractService" class="org.sean.fugao.data.service.impl.AbstractService" abstract="true"> <property name="customerMapper" ref="customerMapperService" /> </bean> <bean id="businessServiceTarget" class="org.sean.fugao.data.service.impl.FugaoServiceImpl" parent="abstractService"/> <bean id="txProxyTemplate" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager" ref="transactionManager"/> <property name="transactionAttributes"> <props> <prop key="save*">PROPAGATION_REQUIRED</prop> <prop key="add*">PROPAGATION_REQUIRED</prop> <prop key="update*">PROPAGATION_REQUIRED</prop> <prop key="remove*">PROPAGATION_REQUIRED</prop> <prop key="get*">PROPAGATION_REQUIRED, readOnly</prop> </props> </property> </bean> <bean id="businessService" parent="txProxyTemplate"> <property name="target" ref="businessServiceTarget"/> </bean> </beans>
5. 同样,也可以在ServiceImple中将businessService Export为bundle service供其他bundle引用
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:osgi="http://www.springframework.org/schema/osgi" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd"> <osgi:service id="fugaoServiceExporter" ref="businessService" interface="org.sean.fugao.data.service.FugaoService"/> </beans>
相关文章推荐
- SSM实战——秒杀系统之DAO层实体定义、接口设计、mybatis映射文件编写、整合Spring与Mybatis
- 【失败】OSGi环境MyBatis3.1.1与Spring3整合
- 【Mybatis从0到1-016】Spring与MyBatis整合mapper开发DAO(推荐使用)
- mybatis3整合spring4-采用mapper动态代理的方式开发Dao
- OSGI企业应用开发(八)整合Spring和Mybatis框架(一)
- Spring和MyBatis环境整合
- Spring和MyBatis环境整合
- Spring和MyBatis环境整合
- mybatis整合spring的 泛型DAO接口
- Spring和MyBatis环境整合
- 移动商城第三篇【搭建Mybatis和Spring环境、编写Dao、Service在Core模块】
- Spring和MyBatis环境整合
- Spring 4.x+Spring MVC 4.x+MyBatis 3.x 整合(一)Spring MVC 3.1.0 环境搭建
- 项目SpringMVC+Spring+Mybatis 整合环境搭建(2)-> 测试Spring+Mybatis 环境
- MyBatis之整合Spring(Dao和Mapper两种方式)
- Mybatis整合Spring之传统dao方法
- Spring和MyBatis环境整合
- AngularJS整合Springmvc、Spring、Mybatis搭建开发环境
- OSGI企业应用开发(八)整合Spring和Mybatis框架(一)
- Spring 和 MyBatis 环境整合