加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 使用Spring AOP的BeanNotOfRequiredTypeException

发布时间:2020-12-15 08:35:10 所属栏目:Java 来源:网络整理
导读:我在 Spring 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.o
我在 Spring 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>

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

Exception in thread “main”
org.springframework.beans.factory.BeanNotOfRequiredTypeException:
Bean named ‘eddie’ must be of type
[com.springinaction.Instrumentalist],
but was actually of type [$Proxy4] at
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:348)
at
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at
org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1008)
at
com.springinaction.Main.main(Main.java:12)

如果我评论< aop:config>它在Spring配置文件中运行得很完美..

为什么会这样?

解决方法

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

在应用程序上下文中的所有bean都已初始化之后,Spring将进行必要的任何后处理.这包括应用AOP建议. Spring将使用代理对象替换名为eddie的bean,在上面的示例中,代理对象在将调用传递给原始对象之前调用另一个对象上的方法.每当你要求名为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);

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读