baidu_32261161 2016-03-02 07:05 采纳率: 0%
浏览 1647

SSH 整合 当Action切面引入事务管理后,属性无法注入,求大神帮忙看一下

没加入事务切面前,Action中的注入costDao可以注入,可是applicationContext.xml加入了一下配置后

<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!--  proxy-target-class指定采用cglib机制生成代理类 --> 
<tx:annotation-driven proxy-target-class="true" transaction-manager="txManager"/>
costDao为打印为空指针,即说明无法注入了。这是为什么?
是否是因为类代理啥的影响了注入?

Action代码如下

package com.action;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;

import com.dao.CostDao;
import com.entity.Cost;

@Controller
@Scope("prototype")
@Transactional(readOnly=true)
public class FindCostAction {

private int page = 1;
private int pageSize;

@Resource(name="costDao")
private CostDao costDao;

private List<Cost> costs;

private int totalPage;

public int getPage() {
    return page;
}

public void setPage(int page) {
    this.page = page;
}

public int getPageSize() {
    return pageSize;
}

public void setPageSize(int pageSize) {
    this.pageSize = pageSize;
}



public int getTotalPage() {
    return totalPage;
}



public void setTotalPage(int totalPage) {
    this.totalPage = totalPage;
}



public List<Cost> getCosts() {
    return costs;
}



public void setCosts(List<Cost> costs) {
    this.costs = costs;
}



public String execute(){

    System.out.println(page);
    System.out.println(pageSize);
    System.out.println(costDao);

    try {
        costs = costDao.findByPage(page, pageSize);
        totalPage = costDao.findTotalPage(pageSize);
    } catch (Exception e) {
        e.printStackTrace();
        return "error";
    }finally{
    }

    return "success";
}

public FindCostAction() {
    super();
    System.out.println("costDao:"+costDao);
}

}

applicationContext.xml配置如下:
<?xml version="1.0" encoding="UTF-8"?>
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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
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/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">
<!-- 定义一个连接池 -->
class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">









<bean id="sessionFactory" 
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <!-- 指定连接池DataSource -->
    <property name="dataSource" ref="myDataSource"/>
    <!-- 指定Hibernate配置参数 -->
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.format_sql">true</prop>
        </props>
    </property>
    <!-- 指定hbm.xml文件 -->
    <property name="mappingResources">
        <list>
            <value>com/entity/Cost.hbm.xml</value>
        </list>
    </property>
</bean>

<!-- 声明事务管理,采用AOP形式切入 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!--  proxy-target-class指定采用cglib机制生成代理类 

-->

<!-- 开启注解扫描 -->
<context:component-scan base-package="com"/>

  • 写回答

1条回答 默认 最新

  • baidu_32261161 2016-03-02 07:39
    关注

    解决了,是要把costDao的get、set方法都写上,可能切面注入的时候需要用到反射加costDao注入到代理类中,而要用到它的get、set方法;
    还是对切面注入等理解不深啊

    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图