Keep Calm and Carry On

SSH框架整合

一、Servlet+Spring

1. 导入jar包

2. 在web.xml中配置Spring配置文件监听

如果在每个业务方法中都是通过 new ClassPathXmlApplicationContext("applicationContext.xml")的方法加载Spring配置文件,获取applicationContext,那么每个方法每次执行都会加载一次Spring的配置文件applicationContext.xml。但是,项目启动后Spring的配置文件应该只需加载一次到内存即可,可以在web.xml中配置一个Spring监听,项目启动就加载一次到内存,之后需要使用便获取即可。

1
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
  1. web.xml配置Spring监听
1
2
3
4
5
6
7
8
9
10
<!--Spring的监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!--Spring配置文件的路径-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
  1. 在需要的时候获取ApplicationContext
1
2
3
4
5
// 从容器中获取UserService
ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());

System.out.println(context.hashCode());
userService = (UserService) context.getBean("userService");

二、Struts+Spring+Hibernate

1. 导入三个框架的jar包

2. Spring配置文件

applicationContext.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring"/>
<property name="user" value="root"/>
<property name="password" value="root"/>
</bean>

<!--SessionFactory,读取hibernate的配置文件hibernate.cfg.xml-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!--<property name="configLocation" value="classpath:hibernate.cfg.xml"/>-->
<!--引入hibernate.cfg.xml的内容,用以删除这个文件-->
<property name="dataSource" ref="dataSource"/>
<!--
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hibernate.connection.autocommit">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="cn/zhuobo/ssh/model/User.hbm.xml"/>
-->
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.connection.autocommit">true</prop>
</props>
</property>
<!--映射文件-->
<property name="mappingLocations" value="classpath:cn/zhuobo/ssh/model/*.hbm.xml"/>
</bean>


<!--HibernateTemplate-->
<bean id="template" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

<!--UserDao-->
<bean id="userDao" class="cn.zhuobo.ssh.dao.impl.UserDaoImpl">
<property name="template" ref="template"/>
</bean>

<!--UserService-->
<bean id="userService" class="cn.zhuobo.ssh.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"/>
</bean>

<!--配置事务:
1. 配置事务管理器
2. 配置一个通知
3. 配置切入点和通知结合
-->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="register"/>
</tx:attributes>
</tx:advice>

<aop:config>
<aop:pointcut id="myPointcut" expression="execution(* cn.zhuobo.ssh.service.impl.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut"/>
</aop:config>

</beans>

3. 测试文件

1
2
3
4
5
6
7
8
9
10
11
12
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class Day01 {
@Autowired
private IUserService userService;

@Test
public void test01() {
User user = new User("Hugh1", "password1", 18);
userService.register(user);
}
}