弹簧型材组 [英] spring profile groups

查看:149
本文介绍了弹簧型材组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,可以为其指定要在其上运行的配置文件. 但我也想将这些配置文件分组为诸如credentails,应用程序性能,内存打印,应用程序行为等. 前任.我可以运行以下配置文件

I have an application, for which I can specify the profiles I want to run it on. but I also want to group these profiles into things like credentails, application performance, memory-print, application behaviour etc. Ex. I can run the following 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

可以在不编写任何自定义代码的情况下实现此目标吗 如何通过属性文件而不是通过env变量或系统属性来设置有效的spring 3.1环境配置文件. 即使可以从属性文件或spring-xml配置中加载这些文件,我也很好. 我在Spring 3.1上仅使用xml配置.

can this be achieved without writing any custom code like How to set active spring 3.1 environment profile via a properites file and not via an env variable or system property. I am fine even if I can load these from properties files or inside spring-xml config. I am using xml only config on spring 3.1.

推荐答案

我不知道没有自定义代码来实现此目标的任何方法,该自定义代码会操纵ConfigurableEnvironment中的活动配置文件.

I don't know of any way to achieve this without custom code which would manipulate the active profiles in a ConfigurableEnvironment.

我们试图在安全框架中实现与权限与角色(权限组)相同的间接模式,但是由于这并不是一厢情愿的,所以我最终不得不解决它.

We're trying to achieve the same indirection pattern as rights vs. roles (group of rights) in a security framework, but since this doesn't come out of the box, I ended up having to work around it.

我保持个人资料的一般性,例如生产和超快的情况,对于每个对那些概要文件敏感的bean,我都设置了正确的@Profile.为了简化重构,我使用了两种技术.

I kept my profiles general, e.g. production and super-fast in your case, and for each bean that is sensitive to those profiles, I set the correct @Profile. To make refactoring easier, I used two techniques.

  1. 为每个配置文件创建一个元注释,例如@Production@SuperFast,并将配置文件名称设为公共常量,例如Production.PROFILE_NAME = "production".
  2. 标记任何bean的概要文件时,如果新的元注释仅适用于一个概要文件,则使用新的元注释;如果适用于多个概要文件,请使用@Profile({Production.PROFILE_NAME, ...}).之所以必须这样做,是因为您不能至少将两个概要文件元注释应用于同一个bean,至少直到4.0 .
  1. Create a meta-annotation for each profile, e.g. @Production, @SuperFast and make the profile name a public constant, e.g. Production.PROFILE_NAME = "production".
  2. When marking the profile of any bean, use your new meta-annotation if it only applies to one profile, or use @Profile({Production.PROFILE_NAME, ...}) if it applies to multiple profiles. You have to do this because you can't apply two profile meta-annotations to the same bean, at least not until 4.0.

例如,

@Profile(Production.PROFILE_NAME)
public @interface Production {

    public static String PROFILE_NAME = "production";
}

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

The point of all this is that you can now use your IDE to look for usages of @Production or Production.PROFILE_NAME if you need to quickly understand or change what beans are being pulled in.

这篇关于弹簧型材组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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