Spring @Profile
创始人
2025-05-30 08:32:39
0

1. Overview

In this tutorial, we’ll focus on introducing Profiles in Spring.

Profiles are a core feature of the framework — allowing us to map our beans to different profiles — for example, dev, test, and prod.

We can then activate different profiles in different environments to bootstrap only the beans we need.


2. Use @Profile on a Bean

Let’s start simple and look at how we can make a bean belong to a particular profile. We use the @Profile annotation — we are mapping the bean to that particular profile; the annotation simply takes the names of one (or multiple) profiles.


Consider a basic scenario: We have a bean that should only be active during development but not deployed in production.

We annotate that bean with a dev profile, and it will only be present in the container during development. In production, the dev simply won’t be active:

@Component
@Profile("dev")
public class DevDatasourceConfig

As a quick sidenote, profile names can also be prefixed with a NOT operator, e.g., !dev, to exclude them from a profile.

In the example, the component is activated only if dev profile is not active:

@Component
@Profile("!dev")
public class DevDatasourceConfig

3. Declare Profiles in XML

Profiles can also be configured in XML. The tag has a profile attribute, which takes comma-separated values of the applicable profiles:




4. Set Profiles

The next step is to activate and set the profiles so that the respective beans are registered in the container.

This can be done in a variety of ways, which we’ll explore in the following sections.

4.1. Programmatically via WebApplicationInitializer Interface

In web applications, WebApplicationInitializer can be used to configure the ServletContext programmatically.

It’s also a very handy location to set our active profiles programmatically:

@Configuration
public class MyWebApplicationInitializer implements WebApplicationInitializer {@Overridepublic void onStartup(ServletContext servletContext) throws ServletException {servletContext.setInitParameter("spring.profiles.active", "dev");}
}

4.2. Programmatically via ConfigurableEnvironment

We can also set profiles directly on the environment:

@Autowired
private ConfigurableEnvironment env;
...
env.setActiveProfiles("someProfile");

4.3. Context Parameter in web.xml

Similarly, we can define the active profiles in the web.xml file of the web application, using a context parameter:

contextConfigLocation/WEB-INF/app-config.xml

spring.profiles.activedev


4.4. JVM System Parameter

The profile names can also be passed in via a JVM system parameter. These profiles will be activated during application startup:

-Dspring.profiles.active=dev

4.5. Environment Variable

In a Unix environment, profiles can also be activated via the environment variable:

export spring_profiles_active=dev

4.6. Maven Profile

Spring profiles can also be activated via Maven profiles, by specifying the spring.profiles.active configuration property.

In every Maven profile, we can set a spring.profiles.active property:

devtruedevprodprod

Its value will be used to replace the @spring.profiles.active@ placeholder in application.properties:

spring.profiles.active=@spring.profiles.active@

Now we need to enable resource filtering in pom.xml:

src/main/resourcestrue...

and append a -P parameter to switch which Maven profile will be applied:

mvn clean package -Pprod

This command will package the application for prod profile. It also applies the spring.profiles.active value prod for this application when it is running.


4.7. @ActiveProfile in Tests

Tests make it very easy to specify what profiles are active using the @ActiveProfile annotation to enable specific profiles:

@ActiveProfiles("dev")

So far, we’ve looked at multiple ways of activating profiles. Let’s now see which one has priority over the other and what happens if we use more than one, from highest to lowest priority:

  • Context parameter in web.xml
  • WebApplicationInitializer
  • JVM System parameter
  • Environment variable
  • Maven profile

5. The Default Profile

Any bean that does not specify a profile belongs to the default profile.

Spring also provides a way to set the default profile when no other profile is active — by using the spring.profiles.default property.


6. Get Active Profiles

Spring’s active profiles drive the behavior of the @Profile annotation for enabling/disabling beans. However, we may also wish to access the list of active profiles programmatically.

We have two ways to do it, using Environment or spring.profiles.active.

6.1. Using Environment

We can access the active profiles from the Environment object by injecting it:

public class ProfileManager {@Autowiredprivate Environment environment;public void getActiveProfiles() {for (String profileName : environment.getActiveProfiles()) {System.out.println("Currently active profile - " + profileName);}  }
}

6.2. Using spring.profiles.active

Alternatively, we could access the profiles by injecting the property spring.profiles.active:

@Value("${spring.profiles.active}")
private String activeProfile;

参考:
Spring Profiles

相关内容

热门资讯

杭州片儿川:本地人早餐的灵魂,... 杭州片儿川作为本地人早餐的灵魂美食,承载着城市的饮食文化与生活记忆。它以雪菜、笋片、肉丝为主要食材,...
小笼包:皮薄汁多的中华名点,先... 小笼包作为中华美食文化的璀璨明珠,以 “皮薄汁多” 的绝妙口感闻名遐迩。这道精致的名点,不仅承载着深...
常州大麻糕:甜咸两种口味,哪款... 常州大麻糕作为江苏常州的传统特色糕点,以甜、咸两种口味闻名遐迩,深受食客喜爱。本文围绕 “常州大麻糕...
琥珀红珠落玉盘:酸甜交织的糖醋... 在中华美食的璀璨星河中,糖醋风味宛如一颗独特的恒星,散发着经久不衰的魅力,而糖醋樱桃肉更是其中令人瞩...
原创 吃... 马齿苋这种不起眼的野菜,在我的餐桌上已经陪伴了整整四十年。记得小时候跟着奶奶去田间地头采摘,她总说这...
品“百家宴”、玩投壶游戏、制作... 为欢庆传统端午佳节,朝阳区常营畅心园社区精心策划了一场主题活动,居民们纷纷贡献厨艺,“百家宴”热气腾...
万般美好,接“粽”而来 | 盘... 端午将至,粽叶飘香 每年端午节吃粽子 不仅仅是味蕾的盛宴, 更是一种文化的传承 但你知道么 粽子还隐...
原创 馄... 标题:馄饨最好吃的做法,比饺子还好吃,隔三差五吃,每次都要吃一大碗。 馄饨,这道传统美食,以其独特...
台北街亮相昆明 吸引民众品尝台... 5月30日,市民手拿刚买的美食自拍。近日,汇聚蚵仔煎、大肠包小肠、黑糖珍珠奶茶、花生卷冰淇淋等经典台...
北方的蒸菜,仿佛开启了一个蔬菜... 在中华饮食文化的宏大版图中,南北方饮食差异一直是人们津津乐道的话题。从对 “青菜” 概念的大相径庭,...
原创 品... 在探讨品酒的奇妙旅程时,我们不得不提到一个有趣的现象:许多人在品鉴葡萄酒时,往往只停留在“三段”的水...
蟹壳黄:咸甜交织的芝麻烧饼,配... 在众多早餐美食中,蟹壳黄与豆腐花堪称绝佳搭配。蟹壳黄,这一咸甜交织的芝麻烧饼,以独特的口感和香气俘获...
原创 鱿... 标题:鱿鱼最好吃的做法,追剧零食必备,香酥入味,就是今年鱿鱼特别贵。 在美食的世界里,鱿鱼以其独特...
原创 鱼... 标题:鱼肉炒猪肉,只有超级大师才做出能吃的 在美食的世界里,每一道菜肴都承载着厨师对食材的尊重与热...
原创 凉... 标题:凉拌莴笋,碧绿爽脆,专治各种没胃口 在炎炎夏日,食欲减退成了许多人的困扰。这时,一道清爽可口...
端午节,你应该知道的“冷知识”... 当农历五月的艾草香飘进街巷 当粽叶包裹的糯米香漫过鼻尖 我们又迎来了端午节 这个和春节、清明节、中秋...
流心巧克力粽、七龙珠大肉粽 成... 封面新闻记者 杨博 摄影报道 近期,端午临近,成都市金牛区马鞍东路粽子一条街又到了一年中最繁忙的时候...
原创 就... 天热就是喜欢做一些汤菜,有汤有菜,有荤有素的吃起来痛快,开胃又下饭。最近我可分享了不少做法了,如随手...
原创 原... 标题:原来河蚌炖不烂是少了这关键的一步,这方法快拿去,简单易学。 在烹饪的世界里,河蚌以其鲜美的肉...
Android kotlin ... 文章目录 一、实现效果二、项目功能三、引入依赖四、三个实体类(分组/分组的子项/展开收起)五、适配器...