如何使用Spring配置基于属性文件配置一个实现?

时间:2016-09-18 20:38:09

标签: java spring

我有一个可以使用服务的两个实现的应用程序:

package test;

public interface MyService {
    void doSomething();
}


package test;

public class MyServiceA implements MyService {

    private final String whatever;

    public MyServiceA(final String whatever) {
        this.whatever = whatever;
    }

    @Override
    public void doSomething() {
        System.out.println("MyServiceA did "+whatever);
    }
}


package test;

public class MyServiceB implements MyService {

    private final String what;

    public MyServiceB(final String what) {
       this.what = what;
    }

    @Override
    public void doSomething() {
        System.out.println("MyServiceB did "+what);
    }
}

具有不同的配置。

我想选择与系统属性一起使用的实现。

我想在每个实现的属性文件中配置每个实现的配置,以及它自己的spring配置。所以我可以在不使用时完全删除所有未使用的配置。

如何在不需要未配置实现的属性文件的情况下配置这两个实现中的任何一个?

欢迎这个问题的其他解决方案。

3 个答案:

答案 0 :(得分:0)

这可以通过@Qualifier来实现。以下链接可以为您提供所需的实施方法。 Spring Qualifier and property placeholder

答案 1 :(得分:0)

您可以为不同的配置文件使用配置文件和不同的@Configuration类。 http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#beans-definition-profiles-java

答案 2 :(得分:0)

我决定将其实施为:https://stackoverflow.com/a/3036044/5759622

这些是必需的spring XML文件:

的applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <import resource="MyService-${MyService}.xml"/>

</beans>

为MyService-MyServiceA.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

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

    <bean class="test.MyServiceA">
        <constructor-arg value="${MyServiceA.whatever}"/>
    </bean>

</beans>

为MyService-MyServiceB.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

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

    <bean class="test.MyServiceB">
        <constructor-arg value="${MyServiceB.what}"/>
    </bean>

</beans>