2016-09-18 66 views
0

我有一个应用程序,可以使用服务的两种实现:如何使用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); 
    } 
} 

具有不同配置。

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

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

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

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

回答

0

我决定实现它:https://stackoverflow.com/a/3036044/5759622

这些都是所需的弹簧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> 
0

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

+0

请注意,第一个答案不是最好的,OP应该读取第二个答案。 – Asoub

+0

@Asoub你应该解释什么问题的答案,而不是说其他​​答案更好 –

0

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

+0

实际上,你可以配置它在你的web.xml或WebApplicationInitializer,而不是系统属性。在web.xml中更改一行并不是真正的硬核方式。 如果你不想为另一个服务提供另一个配置,为什么你需要属性?实际上,当你切换配置文件时,spring甚至不会为非活动配置文件创建一个@Configuration类bean,所以你不需要任何属性,它只会保持镇定) –

+0

因为你需要不同的属性文件不同的bean可以声明另一个PropertyPlaceHolderConfigurer bean并为其属性设置一个前缀。然后你可以使用你的替代bean的前缀属性 –

+0

我不应该修改war应用程序,所以我不能更改web.xml文件。我只能在外部进行配置。 –

相关问题