我尝试在 MethodInvokingFactoryBean
上编写测试程序,有 2 个 bean 声明如下。
<bean id="test2" class="test3">
</bean>
<bean id="m2" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean" lazy-init="false" depends-on="test4">
<property name="targetObject"><ref local="test2"></ref></property>
<property name="targetMethod"><value>execute</value></property>
<property name="arguments">
<list>
<value>abc</value>
<ref local="test4"></ref>
</list>
</property>
</bean>
<bean id="test4" class="test5"></bean>
<bean id="m3"class="org.springframework.beans.factory.config.MethodInvokingFactoryBean" lazy-init="true">
<property name="targetObject"><ref local="test4"></ref></property>
<property name="targetMethod"><value>execute</value></property>
<property name="arguments">
<list>
<value>xyz</value>
</list>
</property>
</bean>
我通过以下方式获取 bean :
test3 obj3 = (test3) _context.getBean("test2");
我的用例是,
在执行方法 execute
之前在 test2 中,我应该能够执行方法 execute
躺在 bean“test4”上,因为“test4”是我运行 execute
的论据之一“test2”中的方法。
而且,我需要制作所有东西lazy-init = true
.
我尝试了上面的方法,但是在执行 test2 中的方法之前,test4 中的方法“execute”没有被调用。
请帮我解决这个问题。
最佳答案
test4
中的
execute
不会被调用,因为 m3 被声明为 lazy-init="true"
。依赖链如下:
- eager bean test2 是独立的
- eager bean test4 是独立的
- eager bean m2 依赖于 test2 和 test4
- lazy bean m3 依赖于 test4
所以在启动时,Spring 初始化 test2、test4,然后初始化 m2。它不执行 m3,因为它很懒。然后,当您向上下文请求 bean test2 时,它会看到 test4 已经完全初始化并返回它。
现在,您说您需要 lazy-init="true"
那样的一切,但实际上您的大多数 Bean 都是渴望的。让一切都变得非常懒惰并以这种方式改变它可以满足您的要求吗?
<bean id="test2" class="test3" lazy-init="true" depends-on="m2">
</bean>
<bean id="m2" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean" lazy-init="true" depends-on="m3">
<property name="targetObject"><ref local="test2"></ref></property>
<property name="targetMethod"><value>execute</value></property>
<property name="arguments">
<list>
<value>abc</value>
<ref local="test4"></ref>
</list>
</property>
</bean>
<bean id="test4" class="test5" lazy-init="true"></bean>
<bean id="m3"class="org.springframework.beans.factory.config.MethodInvokingFactoryBean" lazy-init="true">
<property name="targetObject"><ref local="test4"></ref></property>
<property name="targetMethod"><value>execute</value></property>
<property name="arguments">
<list>
<value>xyz</value>
</list>
</property>
</bean>
这样,一切都是惰性初始化的,因此 Spring 在启动时默认不应该进行任何初始化。现在,依赖链是:
- lazy bean test4 是独立的
- lazy bean m3 依赖 bean test4
- lazy bean m2 依赖于 bean test2、m3 和 test4
- lazy bean test2 依赖于 bean m2
因此,当您请求 bean test2 时,将触发 m2、m3 和 test4 的初始化。它不太漂亮,因为 test2 和 m2 之间存在循环依赖,但它应该可以工作。
无论如何,你的设置相当复杂,如果你不能简化它,我认为你正处于 Spring 开始妨碍你的地步。我建议您用 Java 编写一个服务来隐藏这些依赖项的管理,这样您就可以删除所有这些 XML 并放置一个简单、直接的 bean 声明。与此类似的东西,其中 test2Factory 隐藏了所有丑陋之处:
<bean id="test2Factory" class="test2FactoryClass">
<property name="test4"><bean class="test5"/></property>
</bean>
<bean id="test2" factory-bean="test2Factory" factory-method="getInstance"/>
更新
这变得很困惑,你可以尝试声明一些人造 bean 来打破循环:
<bean id="rawTest2" class="test3" lazy-init="true">
</bean>
<bean id="m2" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean" lazy-init="true" depends-on="m3">
<property name="targetObject"><ref local="rawTest2"></ref></property>
<property name="targetMethod"><value>execute</value></property>
<property name="arguments">
<list>
<value>abc</value>
<ref local="test4"></ref>
</list>
</property>
</bean>
<bean id="test2" class="org.springframework.beans.factory.config.PropertyPathFactoryBean" lazy-init="true" depends-on="m2">
<property name="targetBeanName" value="rawTest2"/>
<property name="propertyPath" value=" "/><!-- One space inside quotes -->
</bean>
<!-- m3 and test4 stay the same as before -->
虽然此时我只会用 Java 编写代码......
关于java - MethodInvokingFactoryBean-方法未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9982274/