当前位置: 代码迷 >> Java Web开发 >> Spring ApplicationContextAware和事务配置有关问题
  详细解决方案

Spring ApplicationContextAware和事务配置有关问题

热度:8478   发布时间:2016-04-11 00:01:21.0
Spring ApplicationContextAware和事务配置问题
这个问题困扰着我几天了.....
//action类
public class BaseAction extends ActionSupport {
public ServiceFacade facade;        //外观类
        public ServiceFacade getFacade() {
return facade;
}
public void setFacade(ServiceFacade facade) {
this.facade = facade;
}
}
//外观类
public class ServiceFacade implements ApplicationContextAware {
        private ApplicationContext ac;
        public ApplicationContext getAc() {
return ac;
}
public void setApplicationContext(ApplicationContext applicationContext){
this.ac = applicationContext;
}
}
//业务层
....

spring配置
    <!-- action配置 -->
<bean id="baseAction" class="cn.warehouse.action.BaseAction">
<property name="facade" ref="facade"></property>
</bean>

    <!-- 外观类 -->
<bean id="facade" class="cn.warehouse.facade.ServiceFacade"></bean>

    <!-- 业务层 -->
         ........


现在问题是声明式事务中
    1. <!-- 声明式事务在业务层 -->  这里完美运行程序,并已经查询到数据到页面(一切正常)
     <aop:config>
      <aop:pointcut expression="execution(* cn.warehouse.service.impl.*.*(..))" id="bizMethod"/>
      <aop:advisor advice-ref="txAdvice" pointcut-ref="bizMethod"/>
     </aop:config>
     
    2.1. <!-- 声明式事务在外观类 -->  报错了
     <aop:config>
      <aop:pointcut expression="execution(* cn.warehouse.facade.*.*(..))" id="bizMethod"/>
      <aop:advisor advice-ref="txAdvice" pointcut-ref="bizMethod"/>
     </aop:config>



异常:
严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'baseAction' defined in class path resource [applicationContext-action.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type [$Proxy0 implementing org.springframework.context.ApplicationContextAware,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [cn.warehouse.facade.ServiceFacade] for property 'facade'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [$Proxy0 implementing org.springframework.context.ApplicationContextAware,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [cn.warehouse.facade.ServiceFacade] for property 'facade': no matching editors or conversion strategy found
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:480)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(Native Method)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:255)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:199)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:45)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4681)
  相关解决方案