John Smith John Smith - 2 months ago 9
Java Question

How to configure one implementation based on a properties file using Spring configuration?

I have an application that can use two implementations of a service:

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);
}
}


with different configurations.

I want to select what implementation to use with a system property.

I want to have the configuration for each implementation in its own property file and also its own spring configuration. So I can remove all the non used configuration altogether when not in use.

How may I configure any of the two implementations without requiring the property file of the non configured implementation?

Other solutions to this problem welcome.

Answer

I decided to implement it as: http://stackoverflow.com/a/3036044/5759622

These are the required spring XML files:

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