继承树的依赖注入

发布于 2024-11-28 02:26:03 字数 2007 浏览 0 评论 0原文

我正在尝试将一些属性注入父类和子类中,但我遇到了一些问题。我想从子级访问注入的 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 技术交流群。

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

发布评论

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

评论(2

失而复得 2024-12-05 02:26:03

我使用直接的 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.

御弟哥哥 2024-12-05 02:26:03

接受的答案肯定是正确的,但您也可以考虑在 spring xml 文件中使用以下内容:

<context:property-placeholder location="classpath:/app.properties" />

然后假设您放置了一个具有正确名称/值对的属性文件,例如

common.address.value=some value
relative.path.value=some/path/value

您可以在 spring xml 中执行类似的操作:

<bean id="parentBean" class="package.Parent">
    <property name="commonAddress" value="${common.address.value}"/>
</bean>

<bean id="childBean" class="package.Child1">
    <property name="relativePath" ref="${relative.path.value}"/>
</bean>

The accepted answer is definitely correct, but you might also consider using the following in your spring xml file:

<context:property-placeholder location="classpath:/app.properties" />

Then assuming you put a properties file with the correct name/value pairs e.g.

common.address.value=some value
relative.path.value=some/path/value

You can do something like this in your spring xml:

<bean id="parentBean" class="package.Parent">
    <property name="commonAddress" value="${common.address.value}"/>
</bean>

<bean id="childBean" class="package.Child1">
    <property name="relativePath" ref="${relative.path.value}"/>
</bean>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文