2010-08-28 120 views
3

我在Spring AOP和下面的Spring配置文件尝试我的手:BeanNotOfRequiredTypeException与Spring AOP

<?xml version="1.0" encoding="UTF-8"?> 
<beans 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" xmlns:aop="http://www.springframework.org/schema/aop" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd 
     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd 
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> 

    <bean id="eddie" class="com.springinaction.Instrumentalist"> 
     <property name="instrument" ref="violin"></property> 
     <property name="song" value="Samarame"></property> 

    </bean> 


    <bean id="kenny" class="com.springinaction.Instrumentalist"> 
     <property name="song" value="SAMARAME "></property> 
     <property name="instrument" ref="saxopone"></property> 
    </bean> 

    <bean id="hank" class="com.springinaction.OneManBand"> 
     <property name="instruments"> 
      <props> 
       <prop key="GUITAR">STRUM STRUM STRUM</prop> 
       <prop key="CYMBAL">CRASH CRASH CRASH CRASH</prop> 
       <prop key="HARMONICA">HUM HUM HUM</prop> 
      </props> 
     </property> 
    </bean> 

    <bean id="guitar" class="com.springinaction.Guitar"> 
    </bean> 

    <bean id="violin" class="com.springinaction.Violin"> 
    </bean> 

    <bean id="tabala" class="com.springinaction.Tabala"> 
    </bean> 

    <bean id="saxopone" class="com.springinaction.Saxophone"> 
    </bean> 

    <bean id="audience" class="com.springinaction.Audience"></bean> 

    <aop:config> 

     <aop:aspect ref="audience"> 

      <aop:before pointcut="execution(* com.springinaction.Performer.perform(..))" method="takeSeats()"/> 
     </aop:aspect> 
    </aop:config> 

</beans> 

当我运行的代码,我得到错误说:

异常在线程“主” org.springframework.beans.factory.BeanNotOfRequiredTypeException: 豆命名为‘埃迪’必须 [com.springinaction.Instrumentalist]类型, 却被类型[$ Proxy4]实际上在 组织.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:348) 在 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) 在 org.springframework.context .support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1008) 在 com.springinaction.Main.main(Main.java:12)

如果我在评论弹簧配置文件<aop:config>元件它是完全运行状态。 。

为什么它是怎么回事?

回答

7

默认情况下,春季使用代理类适用AOP。代理类是动态创建的,以实现多个接口。你传给它一个'处理器'对象,然后当它调用这些接口方法时调用它。您可以阅读代理对象的Javadoc here

在初始化应用程序上下文中的所有bean之后,Spring将执行任何必要的后处理。这包括应用AOP建议。 Spring将与名称eddie与代理对象,在你上面的例子,调用另一个对象的方法传递呼叫到原来的对象之前更换豆。无论何时您要求名称为eddie的bean,您都将获得代理对象而不是真实对象。

我找不到上面堆栈跟踪底部提到的Main类的源代码,但我确实发现了代码here的其余大部分代码。总之,在Main类,看来你正在做的事情一样

Instrumentalist eddie = (Instrumentalist) appContext.getBean("eddie", Instrumentalist.class); 

Spring应用程序上下文的getBean(String, Class)方法检查返回的bean是指定的类的,如果没有,则抛出异常。这就是你上面例子中发生的事情。代理对象不是Instrumentalist一个实例,这就是所谓的$Proxy4自己的代理类的一个实例。 (此代理类不能是Instrumentalist一个子类,因为所有的代理类延伸java.lang.reflect.Proxy)。

代理类将始终贯彻与他们创建的所有接口。 Spring会注意到Instrumentalist实现了Performer,所以它创建的代理类也将实现Performer。你可以用

Performer eddie = (Performer) appContext.getBean("eddie", Performer.class); 

替换上面的线,只要你只需要调用perform()方法上eddie,你的代码应该工作。

+0

感谢Pourquoi Litytestdata为您详细的解答...... 好像你知道有很多关于SpringIdol竞争:) 我想我需要再次了解代理类... 感谢您的澄清... – javanoob 2010-08-28 15:50:48