【问题标题】:How to configure one implementation based on a properties file using Spring configuration?如何使用 Spring 配置基于属性文件配置一种实现?
【发布时间】:2017-01-26 12:15:18
【问题描述】:

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

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 配置中拥有每个实现的配置。所以我可以在不使用时将所有未使用的配置完全删除。

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

欢迎其他解决此问题的方法。

【问题讨论】:

    标签: java spring


    【解决方案1】:

    我决定将其实现为: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>
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 其实你可以在你的 web.xml 或者 WebApplicationInitializer 中配置它,而不是系统属性。在 web.xml 中更改一行并不是真正的硬核方式。如果你不想为另一个服务提供另一个配置,你为什么需要属性?实际上,当您切换配置文件时,spring 甚至不会为非活动配置文件创建 @Configuration 类 bean,因此您不需要任何属性,它只会在它的包中保持冷静)
      • 而且由于您需要为不同的 bean 提供不同的属性文件,您可以声明替代的 PropertyPlaceHolderConfigurer bean 并为其属性设置前缀。然后你可以为你的替代 bean 使用前缀属性
      • 我不应该修改 war 应用程序,所以我无法更改 web.xml 文件。我只能在外部配置。
      【解决方案3】:

      这可以通过@Qualifier 来实现。这是可以为您提供您正在寻找的实施方法的链接。 Spring Qualifier and property placeholder

      【讨论】:

      • 请注意,第一个答案不是最好的,OP 应该阅读第二个。
      • @Asoub 你应该解释一个答案有什么问题,而不是说另一个答案更好
      猜你喜欢
      • 1970-01-01
      • 2011-11-30
      • 2011-03-21
      • 1970-01-01
      • 2017-02-15
      • 2014-04-14
      • 2012-05-25
      • 2017-05-23
      • 1970-01-01
      相关资源
      最近更新 更多