gpt4 book ai didi

java - Spring ProxyFactoryBean注入(inject)问题

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:56:33 24 4
gpt4 key购买 nike

我有一个 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>

我正在尝试将这个 bean 注入(inject)到另一个带有 @Resource 注释的 bean 中,这是我的代码:

@Resource
public ProxyFactoryBean sendSingleSmsServiceProxy;

但我得到这个异常(exception):

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]

如有任何帮助,我们将不胜感激。

最佳答案

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

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

@Resource
public SendSingleSmsService sendSingleSmsService;

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

关于java - Spring ProxyFactoryBean注入(inject)问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4275576/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com