ssh

配置环境spring4+hibernate4+struts2

首先在web.xml文件中加下面这行,默认会在applicationContext.xml文件中加载配置。applicationContext.xml要放在WEB-INF下。

<!--监听器,初始化spring容器 -->
      <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
  • 1
  • 2
  • 3
  • 4

主要是配置applicationContext.xml里的东西,struts.xml里的基本不用改,唯一需要改的是下面这行:

<action name="Login" class="loginAction">
  • 1

将全限定类名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
  • 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

注意事项: 
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) 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

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>
  • 1
  • 2

然后在使用的方法(一般是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;

4.idea中解决问题的办法

报错之后先找第一个错误,第一次出现严重二字的下方
检查该处代码对应的地方 是否有错误 
如果没有 找下方出现caused by的地方 caused by 代表问题出现的原因

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值