开发者

Dependency Injection for Inheritance Tree

开发者 https://www.devze.com 2023-03-26 11:42 出处:网络
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 t

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 testParentInjectionInT开发者_开发问答estClass(){
        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>


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.


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消