sshj2整合步骤

来源:互联网 发布:行业差距 知乎 编辑:程序博客网 时间:2024/05/18 01:42
一,集成 Spring 与 Hibernate1,配置SessionFactory1,配置---------------------- 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:///itcastoadriverClass = com.mysql.jdbc.Driverusername = rootpassword = 12342,测试代码@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类@Servicepublic class InsertUserService {@Resourceprivate SessionFactory sessionFactory;@Transactionalpublic 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>*.action</url-pattern></filter-mapping>2,LazyInitializationException异常说明1,对于集合属性,默认是lazy="true"的,在第一次使用时才加载。2,但在加载时,如果Session已经关掉了就会抛LazyInitializationException异常二,集成 Spring 与 Struts2.1.8.11,在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,拷贝一个用于整合的jar包(与Spring整合用的一个插件)struts-2.1.8.1/lib/struts2-spring-plugin-2.1.8.1.jar3,测试1,写Action类与Service类@Controller("testAction")@Scope("prototype")public class TestAction extends ActionSupport {@Resourceprivate TestService testService;@Overridepublic String execute(){testService.saveTwoUser();return SUCCESS;}}@Servicepublic class TestService {@Resourceprivate SessionFactory sessionFactory;@Transactionalpublic 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 // 测试ProcessEnginepublic void testProcessEngine() {ProcessEngine processEngine = (ProcessEngine) ac.getBean("processEngine");Assert.assertNotNull(processEngine);}