gpt4 book ai didi

java - BeanNotOfRequiredTypeException 与 Spring AOP

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:35:14 27 4
gpt4 key购买 nike

我正在尝试使用 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。动态创建代理类以实现多个接口(interface)。您将一个“处理程序”对象传递给它,然后在调用这些接口(interface)方法中的任何一个时调用该对象。您可以阅读代理对象的 Javadoc here .

在应用程序上下文中的所有 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)。

代理类将始终实现它们创建时使用的所有接口(interface)。 Spring会注意到Instrumentalist实现了Performer,所以它创建的代理类也会实现Performer。您可以将上面的行替换为

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

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

关于java - BeanNotOfRequiredTypeException 与 Spring AOP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3590512/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com