Spring profiles

Spring profiles allow to create and run a separate configuration per environment. A common use case is the declaration of multiple data source configuration beans according to the environment (h2 for development purposes, PostgreSQL in production).

To enable any class / method for a given profile, simply annotate the class/bean method with @Profile("..."):

Any component (@Component / @Configuration) annotated with @Profile("...") will be loaded if the given profile(s) is/are enabled.
This behavior is achieved by using @Conditional(ProfileCondition.class) on the @Profile annotation itself.

As you can see, the ProfileCondition simply check the given value with current environment profile:

Use Spring profiles in JSP

It may be useful to display a piece of content in a JSP file, based on a specific environment profile.
In my use case, I wanted to display an admin-login button to facilitate tests, only in development phase (development profile).

I found that the best way to achieve this behavior was to develop a custom JSP tag as the Servlet class gives helpers to show/hide a piece of text.

The main concern was to find out how to access Spring profiles inside a tag. Fortunately, Spring provides with a useful tag class: RequestContextAwareTag.

Access Spring profiles in a tag

To gain access to the Spring context, you need your tag to extend RequestContextAware class, which
exposes the current “RequestContext” according to the JavaDoc: