使用Spring进行Maven构建和依赖注入 [英] Maven builds and dependency injection using Spring

查看:449
本文介绍了使用Spring进行Maven构建和依赖注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Maven管理的Java应用程序.该项目涉及做某事的国家/地区的概念,例如

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

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

根据我对春天的了解,我可能会每个国家/地区都有一个Spring配置文件.似乎可以使用 Maven配置文件选择适当的配置文件使用配置文件中的元素.但是,文档中说,配置文件旨在支持在不同的环境中构建,而不是具有不同的应用程序配置.

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 :)

编辑:最终,我希望能够为每个国家/地区创建一个.jar.例如

Ultimately I'd like to be able to create a .jar for each country. e.g.

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

推荐答案

我可以看到一些解决方案……

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

如果最终每个国家/地区只有一个JAR文件,正确的方法是在每个国家/地区创建一个单独的Maven项目.有一个定义接口的通用项目,然后有一个国家/地区一个单独的项目,其中包含实现类,作为依赖项的接口项目以及任何所需的其他功能或配置.这样,您可以干净地分离各种实现,而不必担心构建设置.它也是可扩展的,如果您要添加新的国家/地区,只需添加另一个项目-您可以放心使用任何现有功能.

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.

如果要将其保留在一个项目中,则需要考虑何时决定使用哪个国家/地区:是在构建时还是在运行时?使用Maven,您将在构建时决定,而Spring是运行时决定.如果您希望能够在运行时进行决定,则可以使用属性文件.在不参考所用国家/地区的情况下,尽可能通用地运行构建,然后让用户或管理员在运行时决定要使用的国家/地区.通过提供一个属性文件,该文件具有对要使用的Spring上下文文件的引用.然后,在您的main方法中,您可以从类路径中加载此属性文件,并确定要加载哪个Spring上下文文件.

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, e.g. 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.

在这种情况下,请在Spring配置中引用一个通用bean名称,即确保无论加载哪个国家特定的Spring上下文文件,它们都包含ID为country的bean.

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

这篇关于使用Spring进行Maven构建和依赖注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆