Spring ProxyFactoryBean注入问题

ayk*_*kut 3 java spring dependency-injection javabeans

我有一个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>
Run Code Online (Sandbox Code Playgroud)

我试图将这个bean注入另一个带有@Resource注释的bean,这是我的代码:

@Resource
public ProxyFactoryBean sendSingleSmsServiceProxy;
Run Code Online (Sandbox Code Playgroud)

但我得到这个例外:

org.springframework.beans.factory.BeanCreationException:创建名为'com.test.webservice.impl.SendSingleSmsImpl'的bean时出错:资源依赖注入失败; 嵌套异常是org.springframework.beans.factory.BeanNotOfRequiredTypeException:名为'sendSingleSmsServiceProxy'的bean必须是[org.springframework.aop.framework.ProxyFactoryBean]类型,但实际上是[$ Proxy24]类型

任何帮助,将不胜感激.

ska*_*man 6

这是对错误的理解ProxyFactoryBean.与所有实现一样FactoryBean,生成的bean不是FactoryBean工厂生成的bean的类型,而是工厂生成的bean的类型(请参阅Spring文档)

在您的情况下,sendSingleSmsServiceProxybean将是类型SendSingleSmsService:

@Resource
public SendSingleSmsService sendSingleSmsService;
Run Code Online (Sandbox Code Playgroud)

ProxyFactoryBean物体是有效透明的,你看到的是什么它生成.