春季简介组

时间:2013-02-06 14:55:03

标签: java spring profiles spring-profiles

我有一个应用程序,我可以为其指定要运行它的配置文件。 但我也希望将这些配置文件分为诸如凭证,应用程序性能,内存打印,应用程序行为等内容。 防爆。我可以运行以下配置文件

-Dspring.profiles.active=production,cached-local,db-connection-pooled...

但我更愿意将其初始化为

-Dspring.profiles.active=production,super-fast
#the above activates method level caches, db connection pooling etc
#super-fast triggered activation of cached-local, db-connection-pooled profiles

-Dspring.profiles.active=dev,low-footprint
#the above dosent enable caching, or db connection pooling

这可以在不编写任何自定义代码的情况下实现 How to set active spring 3.1 environment profile via a properites file and not via an env variable or system property。 我很好,即使我可以从属性文件或spring-xml配置中加载它们。 我在Spring 3.1上使用xml only config。

1 个答案:

答案 0 :(得分:0)

如果没有自定义代码可以操纵ConfigurableEnvironment中的有效配置文件,我不知道有任何方法可以实现此目的。

我们试图在安全框架中实现与权限与角色(权限组)相同的间接模式,但由于这不是开箱即用的,我最终不得不解决它。

我保持我的个人资料一般,例如在你的情况下生产和超快,对于那些对这些配置文件敏感的bean,我设置了正确的@Profile。为了使重构更容易,我使用了两种技术。

  1. 为每个配置文件创建元注释,例如@Production@SuperFast并将个人资料名称设为公共常量,例如Production.PROFILE_NAME = "production"
  2. 标记任何bean的配置文件时,如果它仅适用于一个配置文件,请使用新的元注释;如果适用于多个配置文件,则使用@Profile({Production.PROFILE_NAME, ...})。您必须执行此操作,因为您无法将两个配置文件元注释应用于同一个bean at least not until 4.0
  3. 例如,

    @Profile(Production.PROFILE_NAME)
    public @interface Production {
    
        public static String PROFILE_NAME = "production";
    }
    

    所有这一切的要点是,如果您需要快速了解或更改正在引入的bean,您现在可以使用IDE查找@ProductionProduction.PROFILE_NAME的用法。