java – 使用Spring AOP的BeanNotOfRequiredTypeException
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 使用Spring AOP的BeanNotOfRequiredTypeException,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3759字,纯文字阅读大概需要6分钟。
内容图文
我在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()方法,那么您的代码应该可以工作.
内容总结
以上是互联网集市为您收集整理的java – 使用Spring AOP的BeanNotOfRequiredTypeException全部内容,希望文章能够帮你解决java – 使用Spring AOP的BeanNotOfRequiredTypeException所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。