继承树的依赖注入
我正在尝试将一些属性注入父类和子类中,但我遇到了一些问题。我想从子级访问注入的 commonAddress 属性,但同时我想在子级中注入相对路径。
父类:
public class Parent {
private String commonAddress;
public void setCommonAddress(String commonAddress) {
this.commonAddress = commonAddress;
}
}
子类:
public class Child1 extends Parent {
private String relativePath;
public void setRelativePath(String relativePath) {
this.relativePath = relativePath;
}
}
src/main/resources 中的 applicationContext.xml:
<bean id="parentBean" class="package.Parent">
<property name="commonAddress" ref="commonAddressString"/>
</bean>
<bean id="childBean" class="package.Child1">
<property name="relativePath" ref="relativePathString"/>
</bean>
src/test/resources 中的 testApplicationContext.xml:
<bean id="commonAddressString" class="java.lang.String">
<constructor-arg>
<value>CommonAddressValue</value>
</constructor-arg>
</bean>
<bean id="relativePathString" class="java.lang.String">
<constructor-arg>
<value>RelativePathValue</value>
</constructor-arg>
</bean>
测试类:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:/applicationContext.xml" })
public class TestParent {
private Parent parent;
public void setParent(Parent parent) {
this.parent = parent;
}
@Test
public void testParentInjectionInTestClass(){
Assert.assertNotNull(parent);
}
}
如果我使用 @Autowired 注释 TestParent 的父属性,则会出现问题,因为有 2 个 bean 符合父类型的条件。
如果我在applicationContext.xml中显式声明测试bean,则断言失败,因此注入不成功。
<bean id="testParent" class="package.TestParent">
<property name="parent" ref="parentBean"></property>
</bean>
I'm trying to inject some properties into a parent class and child class, and i'm facing some problems. I want to have access to the injected commonAddress property from the child, but in the same time I want to inject the relative path in child.
The parent class:
public class Parent {
private String commonAddress;
public void setCommonAddress(String commonAddress) {
this.commonAddress = commonAddress;
}
}
The child class:
public class Child1 extends Parent {
private String relativePath;
public void setRelativePath(String relativePath) {
this.relativePath = relativePath;
}
}
The applicationContext.xml from src/main/resources:
<bean id="parentBean" class="package.Parent">
<property name="commonAddress" ref="commonAddressString"/>
</bean>
<bean id="childBean" class="package.Child1">
<property name="relativePath" ref="relativePathString"/>
</bean>
The testApplicationContext.xml from src/test/resources:
<bean id="commonAddressString" class="java.lang.String">
<constructor-arg>
<value>CommonAddressValue</value>
</constructor-arg>
</bean>
<bean id="relativePathString" class="java.lang.String">
<constructor-arg>
<value>RelativePathValue</value>
</constructor-arg>
</bean>
The test class:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:/applicationContext.xml" })
public class TestParent {
private Parent parent;
public void setParent(Parent parent) {
this.parent = parent;
}
@Test
public void testParentInjectionInTestClass(){
Assert.assertNotNull(parent);
}
}
If I annotate the parent property from TestParent with @Autowired, there is a problem because there are 2 beans that are eligible for Parent type.
If I explicitly declare the test bean in applicationContext.xml, the assertion fails, so the injection is unsuccesful.
<bean id="testParent" class="package.TestParent">
<property name="parent" ref="parentBean"></property>
</bean>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我使用直接的 XML Spring 配置,没有注释。在你的情况下,我只是指定按名称自动装配。我相信通过注释,您可以使用 @Qualifier 实现相同的效果(按名称而不是按类型连接)。
I'm using straight XML Spring configuration without annotations. In your case I would just specify to autowire by name. I believe with the annotations you can achieve the same effect (wiring by name rather than by type) using @Qualifier.
接受的答案肯定是正确的,但您也可以考虑在 spring xml 文件中使用以下内容:
然后假设您放置了一个具有正确名称/值对的属性文件,例如
您可以在 spring xml 中执行类似的操作:
The accepted answer is definitely correct, but you might also consider using the following in your spring xml file:
Then assuming you put a properties file with the correct name/value pairs e.g.
You can do something like this in your spring xml: