范围代理和父代的组合

时间:2016-04-17 13:41:35

标签: spring dependency-injection spring-ioc spring-io

您好我有以下简短代码:

https://github.com/shmuel-buchnik/scope-issue

我收到以下错误:

"无效的属性' targetBeanName' bean类[C]:Bean属性' targetBeanName'不可写或具有无效的setter方法。 setter的参数类型是否与getter的返回类型匹配?"

我很乐意理解。

提前致谢。

添加上下文文件以保存对github的访问

<?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:aop="http://www.springframework.org/schema/aop"
   xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

<bean name="a" class="A">
 <property name="action" ref="c"/>
    </bean>
<bean name="b" class="B" scope="prototype">
    <property name="d" ref="d"/>
    <aop:scoped-proxy proxy-target-class="false"/>
 </bean>
<bean name="c" class="C" parent="b" scope="prototype">
    <aop:scoped-proxy proxy-target-class="false"/>
</bean>
<bean name="d" class="D"/>


</beans>

2 个答案:

答案 0 :(得分:0)

调试后这是问题所在:

在spring中定义父级时,意味着您要继承父bean配置。

定义范围代理时,代理bean包含两个属性targetBeanName和ProxyTargetClass。

当你继承一个作为范围代理的bean时,你将这些属性作为合并父bean配置的一部分。然后你的bean尝试找到一个setter来设置属性并引发异常。

这意味着在我们的示例中,即使c不是作用域代理,我们仍然会得到异常。

答案 1 :(得分:0)

如果将bean定义配置为<aop:scoped-proxy>,则不能将bean定义用作

因此,只需删除<aop:scoped-proxy> bean的b声明,它就可以了。

相关问题