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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器