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例子:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans 3 xmlns="http://www.springframework.org/schema/beans" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xmlns:p="http://www.springframework.org/schema/p" 6 xmlns:context="http://www.springframework.org/schema/context" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 11 12 <!-- Property Place Holder Configuration --> 13 <context:property-placeholder location="classpath:configuration.properties"/> 14 15 <!-- Data Source Configuration --> 16 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 17 <property name="driverClassName" value="${jdbc.driverClassName}"/> 18 <property name="url" value="${jdbc.url}" /> 19 <property name="username" value="${jdbc.username}" /> 20 <property name="password" value="${jdbc.password}" /> 21 <property name="initialSize" value="${jdbc.initialSize}" /> 22 <property name="maxActive" value="${jdbc.maxActive}" /> 23 <property name="maxWait" value="${jdbc.maxWait}" /> 24 </bean> 25 26 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 27 <property name="dataSource" ref="dataSource"/> 28 </bean> 29 30 <!-- SQL Session Factory Configuration --> 31 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 32 <property name="dataSource" ref="dataSource"/> 33 <property name="configLocation" value="classpath:fugaobatis.xml" /> 34 </bean> 35 36 <!-- Mapper Bean --> 37 <bean id="customerMapper" scope="bundle" class="org.mybatis.spring.mapper.MapperFactoryBean"> 38 <property name="mapperInterface" value="org.sean.fugao.data.persistence.CustomerMapper"/> 39 <property name="sqlSessionFactory" ref="sqlSessionFactory"/> 40 </bean> 41 </beans>
2. DAOImpl Bundle必须将Mapper和TransactionManager Export为Bundle Service,那么在ServiceImpl中才能使用Mapper访问数据库,事务也才能定义在ServiceImpl的方法调用之上。
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:osgi="http://www.springframework.org/schema/osgi" 5 xsi:schemaLocation=" 6 http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/osgi 9 http://www.springframework.org/schema/osgi/spring-osgi.xsd"> 10 11 <osgi:service ref="transactionManager" interface="org.springframework.transaction.PlatformTransactionManager" /> 12 13 <bean id="customerMapperExporter" class="org.springframework.osgi.service.exporter.support.OsgiServiceFactoryBean"> 14 <property name="target"> 15 <ref bean="customerMapper"/> 16 </property> 17 <property name="interfaces"> 18 <list> 19 <value>org.sean.fugao.data.persistence.CustomerMapper</value> 20 </list> 21 </property> 22 </bean> 23 </beans>
3. ServiceImpl Bundle会引用DaoImpl Bundle中定义的transaction Manager和customer mapper(通过osgi:reference标签和OsgiServiceProxyFactoryBean来实现)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:osgi="http://www.springframework.org/schema/osgi" 5 xsi:schemaLocation=" 6 http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/osgi 9 http://www.springframework.org/schema/osgi/spring-osgi.xsd"> 10 11 12 <osgi:reference id="transactionManager" interface="org.springframework.transaction.PlatformTransactionManager"/> 13 14 <bean id="customerMapperService" class="org.springframework.osgi.service.importer.support.OsgiServiceProxyFactoryBean"> 15 <property name="beanName" value="customerMapper"/> 16 <property name="interfaces"> 17 <list> 18 <value>org.sean.fugao.data.persistence.CustomerMapper</value> 19 </list> 20 </property> 21 </bean> 22 </beans>
4. ServiceImpl Bundle中Service的定义和Spring申明式事务
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans 3 xmlns="http://www.springframework.org/schema/beans" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xmlns:p="http://www.springframework.org/schema/p" 6 xmlns:context="http://www.springframework.org/schema/context" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 11 12 <bean id="abstractService" class="org.sean.fugao.data.service.impl.AbstractService" abstract="true"> 13 <property name="customerMapper" ref="customerMapperService" /> 14 </bean> 15 16 <bean id="businessServiceTarget" class="org.sean.fugao.data.service.impl.FugaoServiceImpl" parent="abstractService"/> 17 18 <bean id="txProxyTemplate" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> 19 <property name="transactionManager" ref="transactionManager"/> 20 <property name="transactionAttributes"> 21 <props> 22 <prop key="save*">PROPAGATION_REQUIRED</prop> 23 <prop key="add*">PROPAGATION_REQUIRED</prop> 24 <prop key="update*">PROPAGATION_REQUIRED</prop> 25 <prop key="remove*">PROPAGATION_REQUIRED</prop> 26 <prop key="get*">PROPAGATION_REQUIRED, readOnly</prop> 27 </props> 28 </property> 29 </bean> 30 31 <bean id="businessService" parent="txProxyTemplate"> 32 <property name="target" ref="businessServiceTarget"/> 33 </bean> 34 </beans>
5. 同样,也可以在ServiceImple中将businessService Export为bundle service供其他bundle引用
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:osgi="http://www.springframework.org/schema/osgi" 5 xsi:schemaLocation=" 6 http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/osgi 9 http://www.springframework.org/schema/osgi/spring-osgi.xsd"> 10 11 <osgi:service id="fugaoServiceExporter" ref="businessService" interface="org.sean.fugao.data.service.FugaoService"/> 12 </beans>