2 baidu 32261161 baidu_32261161 于 2016.03.02 15:05 提问

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
baidu_32261161   2016.03.02 15:39

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

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!