简体   繁体   中英

Maven builds and dependency injection using Spring

I have a Java application that is managed using Maven. The project involves the concepts of countries that doSomething eg

public interface ICountry {
    public void doSomething();
}

public class England implements ICountry {...}
public class Brazil  implements ICountry {...}

public class CountryApp {
    public static void main (String args[]) {

        ICountry country = null;

        // PSEUDOCODE:
        // Retrieve a chosen implementation of ICountry using Spring

        country.doSomething();
    }
}

From what I've seen of Spring I could potentially have one Spring configuration file for each country. It looks possible to use Maven profiles to select the appropriate configuration file using the element within the profile. However the documentation says profiles are intended for supporting building on different environments rather than having different configurations of the application.

Does this sound like a reasonable approach? If someone knows of a better or "more standard" way please let me know :)

EDIT: Ultimately I'd like to be able to create a .jar for each country. eg

  • myapp-england.jar,
  • myapp-brazil.jar, etc

I can see a couple of solutions for this...

If you want to end up with one JAR file per country, the right way would be to create a separate Maven project per country. Have a common project that defines the interface, and then a separate project per country, which contains the implementation class, the interface project as a dependency and any required additional functionality or configuration. This way, you can cleanly separate the various implementations and don't have to worry about the build set up. It's also extensible, in that you simply have to add another project if you want to add a new country - you're safe of breaking any existing functionality.

If you want to keep it in one project, you need to think about when to make the decision about which country to use: Is it at build time, or at runtime? With Maven, you'll have the decision at build time, whereas Spring is a runtime decision. If you want to be able to decide at runtime, a properties file is probably the way to go. Run the build as generic as possible, without any reference to the used country, and then let the user or administrator decide which country to use at runtime, eg by providing a properties file which has a reference to the Spring context file to use. In your main method, you could then load this properties file from the classpath and decide which Spring context file to load.

In this case, refer to a common bean name in the Spring configuration, ie make sure that regardless of which country-specific Spring context file you load, that they all contain a bean with the ID country .

Using profiles you can run a program with different implementations indicating the implementation to run in a variable controlled by Maven profiles. I have done this in few projects, but one code, one jar. Build a different jar for each implementation is more complicated.

A better approach would be to have a Maven multi-project, I mean, a project with few pom.xml files, each one with its own configuration and implementation.

- Parent project 
  - common project (interfaces and common codec)
  - country 1 project (with dependency of common project)
  - country 2 project (with dependency of common project)
  - ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM