使用Spring AOP的BeanNotOfRequiredTypeException

jav*_*oob 3 java spring spring-aop

我正在春天aop和spring配置文件下面尝试我的手:

<?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>
Run Code Online (Sandbox Code Playgroud)

当我运行代码时,我收到错误说:

异常在线程"主要" org.springframework.beans.factory.BeanNotOfRequiredTypeException:豆命名为"埃迪"的类型必须为[com.springinaction.Instrumentalist],但实际上类型[$ Proxy4]在org.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>在spring配置文件中注释元素它运行完美..

为什么会这样?

Luk*_*ard 7

默认情况下,Spring通过使用代理类来应用AOP .动态创建代理类以实现许多接口.你传递一个'handler'对象,然后在调用任何这些接口方法时调用它.您可以在此处阅读代理对象的Javadoc .

在应用程序上下文中的所有bean都已初始化之后,Spring将进行必要的任何后处理.这包括应用AOP建议.Spring将eddie使用代理对象替换bean ,在上面的示例中,代理对象在将调用传递给原始对象之前调用另一个对象上的方法.每当你要求bean的名称时eddie,你将获得代理对象而不是真实对象.

我找不到源到Main在上面的堆栈跟踪的底部所提及的类,但我发现大部分的代码的其余部分在这里.无论如何,在Main课堂上,你似乎在做类似的事情

Instrumentalist eddie = (Instrumentalist) appContext.getBean("eddie", Instrumentalist.class);
Run Code Online (Sandbox Code Playgroud)

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

代理类将始终实现它们创建的所有接口.Spring会注意到Instrumentalistimplements Performer,因此它创建的代理类也将实现Performer.你可以用上面的代替

Performer eddie = (Performer) appContext.getBean("eddie", Performer.class);
Run Code Online (Sandbox Code Playgroud)

并且,如果您只需要调用该perform()方法eddie,您的代码应该可以工作.