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.
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
Profiles can also be configured in XML. The tag has a profile attribute, which takes comma-separated values of the applicable 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.
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");}
}
We can also set profiles directly on the environment:
@Autowired
private ConfigurableEnvironment env;
...
env.setActiveProfiles("someProfile");
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.active dev
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
In a Unix environment, profiles can also be activated via the environment variable
:
export spring_profiles_active=dev
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
:
dev true dev prod prod
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/resources true ...
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.
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:
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.
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
.
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);} }
}
Alternatively, we could access the profiles by injecting the property spring.profiles.active:
@Value("${spring.profiles.active}")
private String activeProfile;
参考:
Spring Profiles
上一篇:DataFun:知识图谱在线峰会
下一篇:端午假期,坐旅游列车去哪儿玩?