Domanda

I am a developer working on a Java web application that is built on the Spring framework. This application will be deployed to several different customers. There is a class which contains some business logic that is different for each client. From a Spring framework point of view, it is enough to simply wire in the appropriate class (as a bean) for each client.

However, the developers are not responsible for deployment of the application. The Operations team is responsible, and having them open up the WAR file and modify the spring configuration XML for each client deployment is probably too much to ask from them. Properties files are ok, but modifying internal files - probably not.

Has anyone else come up with a strategy for dealing with this?

Edit:

To give an example of what I'm talking about:

public interface IEngine {
  void makeNoise();
}

public class Car {
  public void setEngine(IEngine engine) {
    this.engine = engine;
  }
}

Customer A's business logic:

HeavyDutyEngine implements IEngine {
  public void makeNoise() {
    System.out.println("VROOOM!");
  }
}

Customer B's business logic:

LightWeightEngine implements IEngine {
  public void makeNoise() {
    System.out.println("putputput");
  }
}

In the Spring configuration XML:

For client A, it might look like this:

<bean id="hdEngine" class="HeavyDutyEngine" />
<bean id="lwEngine" class="LightWeightEngine" />

<bean id="car" class="Car">
  <property name="engine" ref="hdDngine">
</bean>

For client B, it might look like this:

<bean id="hdEngine" class="HeavyDutyEngine" />
<bean id="lwEngine" class="LightWeightEngine" />

<bean id="car" class="Car">
  <property name="engine" ref="lwEngine">
</bean>
È stato utile?

Soluzione

To configure Spring for different environments, you can use the concept called spring "profiles". (introduced in Spring 3.1)

You have different ways so enable/disable this properties. For example java properties parameter. But because you are using a WAR, and therefore some Servlet container, I would recommend to put this configuration in the Servlet container.

In a tomcat for example, you can put this line in the context.xml (global or application specific) to enable a profile

<Parameter name="spring.profiles.active" value="myProfile"/>

Altri suggerimenti

You can move some configuration to DB, if you are using one.

And you can use something like this

 ref="{ref-name}"

where ref-name can be resolved using properties file(default) by configuring PropertyPlaceholderConfigurer.

Or you can write your own wrapper over PropertyPlaceholderConfigurer which will take the values from DB table which is external to you deployable WAR file.

In one of mine project, we used this method to resolve custom dependencies. The wrapper which looks up the DB used to take first priority and if the DB doesn't have the key/value pair, then properties file (bundled in the WAR) was used to resolve dependencies.

This will also allow you to change some value externally from DB, however with IBM Websphere we need to recycle the server for changes to take place.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top