【发布时间】:2011-03-22 17:14:26
【问题描述】:
通常通过以下配置 (http://static.springsource.org/sprin...beans-beanname) 通过 setter 注入依赖项:
<bean id="exampleBean" class="examples.ExampleBean">
<!-- setter injection using the nested <ref/> element -->
<property name="beanOne"><ref bean="anotherExampleBean"/></property>
<!-- setter injection using the neater 'ref' attribute -->
<property name="beanTwo" ref="yetAnotherBean"/>
<property name="integerProperty" value="1"/>
</bean>
<bean id="anotherExampleBean" class="examples.AnotherBean"/>
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>
假设类 examples.ExampleBean 有一个集合侦听器对象,而方法 addListener(SomeListenerInterface) 是添加侦听器的唯一可能方法。我可以像使用属性设置器那样在 xml 中以声明方式注入侦听器吗?
【问题讨论】:
标签: java spring dependency-injection inversion-of-control