OA中SSH+JBPM项目整合
一,集成 Spring 與 Hibernate
?1,配置SessionFactory
??1,配置
???---------------------- applicationContext.xml ------------------------
???<!-- 配置SessionFactory(整合Hibernate) -->
???<context:property-placeholder location="classpath:jdbc.properties" />
???<bean id="sessionFactory"
????class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
????<property name="dataSource">
?????<bean class="com.mchange.v2.c3p0.ComboPooledDataSource">
??????<!-- 數據庫連接信息 -->
??????<property name="jdbcUrl" value="${jdbcUrl}"></property>
??????<property name="driverClass" value="${driverClass}"></property>
??????<property name="user" value="${username}"></property>
??????<property name="password" value="${password}"></property>
??????<!-- 其他配置 -->
??????<property name="initialPoolSize" value="3"></property>
??????<property name="maxPoolSize" value="5"></property>
??????<property name="minPoolSize" value="3"></property>
??????<property name="acquireIncrement" value="2"></property>
??????<property name="maxStatements" value="8"></property>
??????<property name="maxStatementsPerConnection" value="5"></property>
??????<property name="maxIdleTime" value="20"></property>
?????</bean>
????</property>
????<!-- 指定hibernate的配置文件的位置 -->
????<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
???</bean>
???---------------------- jdbc.properties ------------------------
???jdbcUrl = jdbc:mysql:///itcastoa
???driverClass = com.mysql.jdbc.Driver
???username = root
???password = 1234
??2,測試代碼
???@Test// 測試 SessionFactory 的配置
???public void testSessionFactory(){
????SessionFactory sessionFactory = (SessionFactory) ac.getBean("sessionFactory");
????Assert.assertNotNull(sessionFactory.openSession());
???}
?2,配置聲明式事務(使用基于注解的方式)
??1,配置
???<!-- 配置事務管理器 -->
???<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
????<property name="sessionFactory" ref="sessionFactory"></property>
???</bean>
???<!-- 配置基于注解的事務支持-->
???<tx:annotation-driven transaction-manager="transactionManager"/>
??2,測試代碼
???1,Service類
????@Service
????public class InsertUserService {
?????@Resource
?????private SessionFactory sessionFactory;
?????@Transactional
?????public void addUsers() {
??????sessionFactory.getCurrentSession().save(new User());
??????// int a = 1 / 0; // 這行會拋異常
??????sessionFactory.getCurrentSession().save(new User());
?????}
????}
???2,單元測試
????@Test // 測試聲明式事務
????public void testTransaction() {
?????InsertUserService service = (InsertUserService) ac.getBean("insertUserService");
?????service.addUsers();
????}
?3,在web.xml中配置 spring 的 OpenSessionInView 過濾器(解決拋LazyInitializationException的問題)
??1,配置
???<!-- 配置 spring 的 OpenSessionInView 過濾器 -->
???<filter>
????<filter-name>OpenSessionInView</filter-name>
????<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
???</filter>
???<filter-mapping>
????<filter-name>OpenSessionInView</filter-name>
????<url-pattern>*.do</url-pattern>
???</filter-mapping>
??2,LazyInitializationException異常說明
???1,對于集合屬性,默認是lazy="true"的,在第一次使用時才加載。
???2,但在加載時,如果Session已經關掉了就會拋LazyInitializationException異常
二,集成 Spring 與 Struts2.1.8.1
?1,在web.xml配置監聽器(Spring Reference 15.2 Common configuration)
??<!-- 集成Spring -->
??<listener>
???<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
??</listener>
??<context-param>
???<param-name>contextConfigLocation</param-name>
???<param-value>/WEB-INF/classes/applicationContext*.xml</param-value>
??</context-param>
?2,在struts-config.xml中配置controller(Spring Reference 15.4.1.1. DelegatingRequestProcessor)
??<!-- 集成Spring -->
??<controller>
???<set-property property="processorClass" value="org.springframework.web.struts.DelegatingRequestProcessor" />
??</controller>
?3,測試
??1,寫Action類與Service類
???@Controller("testAction")
???@Scope("prototype")
???public class TestAction extends ActionSupport {
????@Resource
????private TestService testService;
????@Override
????public String execute(){
?????testService.saveTwoUser();
?????return SUCCESS;
????}
???}
???@Service
???public class TestService {
????@Resource
????private SessionFactory sessionFactory;
????@Transactional
????public void saveTwoUser() {
?????sessionFactory.getCurrentSession().save(new User());
?????// int a = 1 / 0; // 這行會拋異常
?????sessionFactory.getCurrentSession().save(new User());
????}
???}
??2,在struts.xml中配置Action
???<!-- 測試 -->
???<action name="test" class="testAction">
????<result>/test.jsp</result>
???</action>
??3,部署到Tomcat中并訪問測試。
?4,說明:
??1,在寫Action時要指定 @Controller 與 @Scope("prototype")
??2,在struts.xml中配置action時,在class屬性中寫bean的名稱
??
三,整合Spring與Jbpm4(jBPM4.4 Developers Guide, Chapter 17. Spring Integration)?
?1,在jbpm.cfg.xml中
??1,刪除配置:
???<import resource="jbpm.tx.hibernate.cfg.xml" />
??2,增加配置:
???<!-- 整合Spring -->
???<import resource="jbpm.tx.spring.cfg.xml" />
?2,在applicationContext.xml中配置
??<!-- 配置ProcessEngine(整合jBPM4) -->
??<bean id="springHelper" class="org.jbpm.pvm.internal.processengine.SpringHelper">
???<!-- jbpmCfg是相對于classpath的相對路徑,默認值為jbpm.cfg.xml -->
???<property name="jbpmCfg" value="jbpm.cfg.xml"></property>
??</bean>
??<bean id="processEngine" factory-bean="springHelper" factory-method="createProcessEngine" />
?3,測試
??@Test // 測試ProcessEngine
??public void testProcessEngine() {
???ProcessEngine processEngine = (ProcessEngine) ac.getBean("processEngine");
???Assert.assertNotNull(processEngine);
??}
?
總結
以上是生活随笔為你收集整理的OA中SSH+JBPM项目整合的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 东风标致锁车后自动落锁键一直在闪?
- 下一篇: OA中基础功能总结