有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Spring ProxyFactoryBean注入问题

我有一个ProxyFactoryBean:

<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>

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

@Resource
public ProxyFactoryBean sendSingleSmsServiceProxy;

但我有一个例外:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.test.webservice.impl.SendSingleSmsImpl': Injection of resource dependencies failed; 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]

任何帮助都将不胜感激


共 (1) 个答案

  1. # 1 楼答案

    这是对ProxyFactoryBean所做工作的错误理解。与FactoryBean的所有实现一样,生成的bean不是FactoryBean的类型,而是工厂生成的任何bean的类型(see Spring docs

    在您的例子中,sendSingleSmsServiceProxybean将是SendSingleSmsService类型:

    @Resource
    public SendSingleSmsService sendSingleSmsService;
    

    ProxyFactoryBean对象实际上是透明的,您看到的是它生成的任何内容