在 ProxyFactoryBean 中注入属性时顺序是否重要

发布于 2024-11-28 04:23:45 字数 1057 浏览 0 评论 0原文

我正在尝试将各个方面注入到服务中。对于这项服务,我使用经典方式创建一个代理对象。

我编写了一个类型为 (ProxyFactoryBean) 的 bean-baseProxy,其中包含所有必需建议的列表。

    <bean id="baseProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="interceptorNames">
        <list>
            <value>methodInvocationAdvice</value>
        </list>
    </property>
</bean>

我正在为这样的服务创建一个代理:

<bean id="singproxy" parent="baseProxy">
    <property name="target" ref="singtarget" />
    <property name="targetClass" value="com.spring.learning.SingingService"></property>
</bean>

这不起作用,但是当我恢复这两个属性并这样写时:

<bean id="singproxy" parent="baseProxy">
    <property name="targetClass" value="com.spring.learning.SingingService"></property>
    <property name="target" ref="singtarget" />
</bean>

令我惊讶的是它工作正常。春天,豆子的顺序重要吗?或者它是 ProxyFactoryBean 的特殊情况? 我尝试使用 Spring 3.0,但不确定以前的版本是否存在相同的行为。

I am trying to inject the aspects in a service. For this service I am creating a proxied object using classic way.

I have written a bean- baseProxy of type (ProxyFactoryBean) which contains a list of all the required advices.

    <bean id="baseProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="interceptorNames">
        <list>
            <value>methodInvocationAdvice</value>
        </list>
    </property>
</bean>

I am creating a proxy for the service like this :

<bean id="singproxy" parent="baseProxy">
    <property name="target" ref="singtarget" />
    <property name="targetClass" value="com.spring.learning.SingingService"></property>
</bean>

Which doesn't work but when I revert these two properties and write like this :

<bean id="singproxy" parent="baseProxy">
    <property name="targetClass" value="com.spring.learning.SingingService"></property>
    <property name="target" ref="singtarget" />
</bean>

To my surprise it works fine. In spring does it matter on the order for bean ? Or its a special case with ProxyFactoryBean?
I tried with Spring 3.0 I am not sure same behavior exists with previous versions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感悟人生的甜 2024-12-05 04:23:45

对于 targettargetClass 来说,它是其中之一,但不能两者兼而有之。这是相关的源代码(来自 org.springframework.aop.framework.AdvisedSupport),它是 ProxyFactoryBean 的父类:

public void setTarget(Object target) {
    setTargetSource(new SingletonTargetSource(target));
}

public void setTargetSource(TargetSource targetSource) {
    this.targetSource = (targetSource != null ? targetSource : EMPTY_TARGET_SOURCE);
}


public void setTargetClass(Class targetClass) {
    this.targetSource = EmptyTargetSource.forClass(targetClass);
}

如您所见,setTarget()setTargetClass() 写入同一字段,因此最后一个分配获胜。

Concerning target and targetClass, It's one or the other, but not both. Here's the relevant source (from org.springframework.aop.framework.AdvisedSupport), a parent class of ProxyFactoryBean:

public void setTarget(Object target) {
    setTargetSource(new SingletonTargetSource(target));
}

public void setTargetSource(TargetSource targetSource) {
    this.targetSource = (targetSource != null ? targetSource : EMPTY_TARGET_SOURCE);
}


public void setTargetClass(Class targetClass) {
    this.targetSource = EmptyTargetSource.forClass(targetClass);
}

As you can see, both setTarget() and setTargetClass() write to the same field, so the last assignment wins.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文