sshj2整合步骤

2023-04-12


一,集成 Spring 与 Hibernate
	1,配置SessionFactory
		1,配置
			---------------------- applicationContext.xml ------------------------
			
			
			
				
					
						
						
						
						
						
						
						
						
						
						
						
						
						
					
				
				
				
			
			---------------------- 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,配置
			
			
				
			
			
			
		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,配置
			
			
				OpenSessionInView
				org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
			
			
				OpenSessionInView
				*.action
			
		2,LazyInitializationException异常说明
			1,对于集合属性,默认是lazy="true"的,在第一次使用时才加载。
			2,但在加载时,如果Session已经关掉了就会抛LazyInitializationException异常

二,集成 Spring 与 Struts2.1.8.1
	1,在web.xml配置监听器(Spring Reference 15.2 Common configuration)
		
		
			org.springframework.web.context.ContextLoaderListener
		
		
			contextConfigLocation
			/WEB-INF/classes/applicationContext*.xml
		
	2,拷贝一个用于整合的jar包(与Spring整合用的一个插件)
		struts-2.1.8.1/lib/struts2-spring-plugin-2.1.8.1.jar
	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
			
			
				/test.jsp
			
		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,删除配置:
			
		2,增加配置:
			
			
	2,在applicationContext.xml中配置
		
		
			
			
		
		
	3,测试
		@Test // 测试ProcessEngine
		public void testProcessEngine() {
			ProcessEngine processEngine = (ProcessEngine) ac.getBean("processEngine");
			Assert.assertNotNull(processEngine);
		}


本文仅代表作者观点,版权归原创者所有,如需转载请在文中注明来源及作者名字。

免责声明:本文系转载编辑文章,仅作分享之用。如分享内容、图片侵犯到您的版权或非授权发布,请及时与我们联系进行审核处理或删除,您可以发送材料至邮箱:service@tojoy.com