- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试使用 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/
我有这样的 bean : 这是类声明: MyClass extends MySuperClass implements MyInterface MySuperClass extends Gener
我正在编写一个小应用程序来检查 spring-AOP 中“AfterAdvice”概念的功能,但我得到一个与 xml 文件相关的异常(我认为)请帮助我如何解决异常 应用程序上下文.xml
我的 Controller 包含以下注释: @Resource(name="userService") private UserDetailsServiceImpl userService; 服务本身
运行测试类会引发以下异常: BeanNotOfRequiredTypeException: Bean named 'myServiceImpl' is expected to be of type '
我需要有关 Spring 和代理问题的帮助。 org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named
我正在尝试使用 spring aop 和 spring 配置文件:
我还是 Spring MVC 的新手,在构建我的测试项目时,我从 Tomcat 日志中收到了这条消息: SEVERE: Exception sending context initialized ev
我的 Spring MVC 项目与 Apache Shiro 一起运行,以确保 Web 和 API 安全。生活很美好,直到有一天我必须进行权限检查以获取授权。 使用@RequiresPermissio
我尝试使用 Spring MVC 和 Hibernate 运行我的第一个 Web 应用程序,但我遇到了一些问题。 通用道: @Repository public abstract class Gene
在遵循 a thread about service beans 中给出的重要建议之后我已经提供了下面列出的服务。我试过将@Transactional 放在接口(interface)级别、接口(int
我有一个简单的(示例)Web 应用程序,如果我尝试链接多个 View 解析器,我会遇到问题。当我的应用程序按照下面描述的方式配置时,尝试访问 http://localhost:8080/Library
我正在尝试使用 BeanPostProcessor 运行 Spring 示例。 下面是bean后处理器 public class DisplayNamePostProcessor implements
我遇到了一个问题,PropertiesFactoryBean 在 spring 中没有被解析为 Properties 对象。相反,我收到以下错误: org.springframework.beans.
我在从应用程序上下文中提取 Spring bean 时遇到问题。 当我尝试时; InnerThread instance = (InnerThread) SpringContextFactory.ge
我有一个触发 hadoop 作业的 spring 批处理作业。请在下面查看我的工作配置。当我启 Action 业时,出现以下异常... 配置: 异常: org.springframework.bea
我有带有 Spring Cloud sleuth 的 spring boot 项目(2.1.0 版本)。Spring Sleuth 的 TracingConnectionFactoryBeanPost
我有这样的类和 Spring 上下文。 如何修复这个错误的 Java 配置,而不是 xml? 我尝试了其他帖子中的一些解决方案,但没有成功。 @Service @Transactional publi
我是一名优秀的程序员,十分优秀!