配置环境spring4+hibernate4+struts2
首先在web.xml文件中加下面这行,默认会在applicationContext.xml文件中加载配置。applicationContext.xml要放在WEB-INF下。
<!--监听器,初始化spring容器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
主要是配置applicationContext.xml里的东西,struts.xml里的基本不用改,唯一需要改的是下面这行:
<action name="Login" class="loginAction">
将全限定类名net.cxp.action.Login改为@Controller(“loginAction”)里写的名字。@Controller注解放在Action上。注意要添加@Scope注解@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE),因为每个请求一个action,而spring生成的bean默认为单例,这句将其改为原型bean,即多实例的bean。
applicationContext.xml文件如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:tx="http://www.springframework.org/schema/tx" xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 打开注解装配并自动检测Bean和定义Bean -->
<context:component-scan base-package="net.cxp"></context:component-scan>
<!-- 定义数据源 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" />
<property name="username" value="cxp" />
<property name="password" value="123" />
</bean>
<!-- 定义session工厂 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>net/cxp/entity/Portfolio.hbm.xml</value>
<value>net/cxp/entity/Puser.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="dialect">org.hibernate.dialect.Oracle9Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<!-- 配置二级缓存 -->
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<!-- 由于查询的缓存命中率很低,所以关掉查询时的二级缓存 -->
<prop key="hibernate.cache.use_query_cache">false</prop>
<!--设置缓存的类型,设置缓存的提供商 -->
<prop key="cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory
</prop>
<!--使用 getCurrentSession()必须配置此属性注意不是thread而是org.springframework.orm.hibernate4.SpringSessionContext -->
<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext
</prop>
</props>
</property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 事务注解驱动 -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
注意事项:
1.要将struts2里的antlr-2.7.2.jar包删掉,因为和hibernate里的antlr-2.7.7.jar冲突了。 注意还要去tomcat下相应的项目下的webinfo/lib里删掉,因为之前加载了两个包不然会报下面的错误
Exception Name:
java.lang.reflect.UndeclaredThrowableException
What you did wrong:
java.lang.reflect.UndeclaredThrowableException
at com.sun.proxy.$Proxy76.createQuery(Unknown Source)
at net.cxp.dao.impl.UserDaoImpl.authenticateUser(UserDaoImpl.java:58)
at net.cxp.biz.impl.UserServiceImpl.authenticateUser(UserServiceImpl.java:32)
at net.cxp.action.Login.execute(Login.java:36)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
2.第二点是配置spring事务管理。虽然无论是hibernate还是spring本质上都是使用jdbc的事务管理,但配置还是要有的。
2.1如果不用spring事务管理的话。
把上面配置事务管理的bean和驱动删掉,sessionFactory里的hibernate.current_session_context_class值改为thread
<!--使用 getCurrentSession()必须配置此属性注意不是thread而是org.springframework.orm.hibernate4.SpringSessionContext -->
<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
然后在使用的方法(一般是dao里的sava,update等方法)中手动打开和提交事务,就算是查询也要,否则会报createQuery is not valid without active transaction的错误。
2.2如果使用spring管理事务。实际上是使用aop完成的。(顺便提一句spring官网提供的包里没有aspectj,要到Aspectj官网下,而官网下的包里好像没有 aopalliance.jar,这个jar使用来织入切面的)
配置spring管理事务
1.在applicationContext.xml文件里写下面的两行
<!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 事务注解驱动 -->
<tx:annotation-driven transaction-manager="transactionManager" />
2.修改sessionFactory bean里hibernate.current_session_context_class的值
<!--使用 getCurrentSession()必须配置此属性注意不是thread而是org.springframework.orm.hibernate4.SpringSessionContext -->
<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext
</prop>
3.在需要事务的方法上加注解@Transactional
3.在action中注入服务对象需要插件struts2-spring-plugin-2.2.1.jar,不然当你调用service里的方法的时候会抛出一个大大的空指针异常
/**
* 在action中注入服务对象需要插件struts2-spring-plugin-2.2.1.jar
* 使用spring注入服务
*/
@Autowired
private UserService userService;
今天突发奇想的想把以前写的东西用ssh改写,所以仅靠spring in action里spring配置hibernate的小例子整合一下。百度了一天,光是丢包少包和包冲突都搞了好久。而且一开始还不打算陪置spring管理事务的,百度时看到这样比较好才又配的。