简体   繁体   English

Spring ProxyFactoryBean注入问题

[英]Spring ProxyFactoryBean Injection Problem

I have a ProxyFactoryBean bean : 我有一个ProxyFactoryBean bean:

<bean id="sendSingleSmsServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
   <property name="target">
      <ref bean="sendSingleSmsServiceImpl" />
   </property>
   <property name="proxyInterfaces">
      <value>com.test.SendSingleSmsService</value>
   </property>
   <property name="interceptorNames">
      <value>hibernateInterceptor</value>
   </property>
</bean>

and I'm trying to inject this bean into another one with @Resource annotation here is my code for that : 我试图将这个bean注入另一个带有@Resource注释的bean,这是我的代码:

@Resource
public ProxyFactoryBean sendSingleSmsServiceProxy;

but I get this exception : 但我得到这个例外:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.test.webservice.impl.SendSingleSmsImpl': Injection of resource dependencies failed; org.springframework.beans.factory.BeanCreationException:创建名为'com.test.webservice.impl.SendSingleSmsImpl'的bean时出错:资源依赖注入失败; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'sendSingleSmsServiceProxy' must be of type [org.springframework.aop.framework.ProxyFactoryBean], but was actually of type [$Proxy24] 嵌套异常是org.springframework.beans.factory.BeanNotOfRequiredTypeException:名为'sendSingleSmsServiceProxy'的bean必须是[org.springframework.aop.framework.ProxyFactoryBean]类型,但实际上是[$ Proxy24]类型

any help would be appreciated. 任何帮助,将不胜感激。

This is a mis-understanding in what ProxyFactoryBean does. 这是对ProxyFactoryBean所做的误解。 Like all implewntations of FactoryBean , the bean that is generated is not the type of the FactoryBean , but the type of whatever bean the factory generates ( see Spring docs ) 与FactoryBean的所有实现FactoryBean ,生成的bean不是FactoryBean的类型,而是工厂生成的任何bean的类型( 请参阅Spring文档

In your case, the sendSingleSmsServiceProxy bean is going to be of type SendSingleSmsService : 在您的情况下, sendSingleSmsServiceProxy bean将是SendSingleSmsService类型:

@Resource
public SendSingleSmsService sendSingleSmsService;

The ProxyFactoryBean object is effectively transparent, what you see is whatever it generates. ProxyFactoryBean对象实际上是透明的,您看到的是它生成的任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM