Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Weblogic 12c : How to pass the "active profile" to a Spring Boot application?

I'm developing a Spring Boot application and I have to make it run in our Weblogic 12c server, as a war.

I try to find a way to pass the default Spring active profile (info here and here) to the application, without having to set an environment variable on the machine itself. I'd like to be able to specify this profile using the Weblogic admin console, if possible.

I tried playing with the "Environment / Servers / MyServer / Server Start / Arguments" options, but I've been unable to make Spring load the specified profile this way. I've tried, without success :

spring.profiles.active=dev
-Dspring.profiles.active='dev'
-Dspring.profiles.active=dev
-spring.profiles.active=dev
--spring.profiles.active=dev

Any idea if it's possible to pass the spring.profiles.active to Spring, using Weblogic? I need to pass "dev", "acc" or "prod".

UPDATE : I would accept an answer explaining any easy way to have different .properties files used for the application configurations, depending on the environment the Sprint Boot application runs on. Those .properties files could be bundled in the application itself, or can be external. But I need to touch the system Weblogic is running on at least as possible... No environment variables and ideally no change to the Weblogic files either! Ideally, the solution would involve some kind of manipulations done using the Weblogic Admin console, for example a way to pass a parameter representing the current environment to the Spring Boot application so the correct application.[ENVIRONMENT].properties file is used.

UPDATE 2 : One way of allowing each environment to have its own application.properties file , is by using Deployment plans. This is probably the recommended way of doing it. I'll add that as an answer, but I'd still prefere an easier approach... I can't believe it's not possible to simply pass a parameter to an application, when it's deployed in Weblogic! And what is the "Environment / Servers / MyServer / Server Start / Arguments option for if it can't be used for that purpose?

UPDATE 3 : Related thread. If I understand correctly, the "Environment / Servers / MyServer / Server Start / Arguments" options may only work if a NodeManager is enabled? That would explain why I'm unable to pass that spring.profiles.active=dev variable!

like image 932
electrotype Avatar asked Nov 04 '16 17:11

electrotype


2 Answers

Although Spring Profiles let us to populate the Spring Environment with the set of properties that belong to the same logical group named as profile, Spring Profiles is not the goal but one of the ways to achieve the goal.

The goal is to be able to populate the Spring Environment from externalized configuration, that is, the goal is to be able to deploy the same application code in different environments and load the specific configuration for each single environment.

Spring Boot offers a way to achieve that goal based on having an own PropertySource that designed to allow sensible overriding of values.

If you read http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config you will see that the profile-specific application properties is just one of the available sources of properties and that JNDI attributes will override the profile-specific properties.

Analyzing the Spring Boot properties overriding order (http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config) for JEE environments, the JNDI attributes take in fact the most precedence:

  1. I advise against using global settings, like global properties files, command line arguments, system environment variables, etc. because they are shared by all the applications deployed in the same application server.
    • You will get a collateral benefit: there is no need to modify Weblogic startup files
  2. ServletConfig and ServletContext init parameters aren't external configuration, to use them implies you have to package the application for each environment.

Note to activate a profile you must use global variable, so all the deployed applications in the same app server will activate the same profile.

So, in practice, in Spring Boot applications for pure JEE environments you have only two configuration sources:

  1. JNDI attributes
  2. Packaged application.properties

Note you should avoid the usage of packaged application-{profile}.properties files because profile activation means to activate the same profile in all the apps deployed in the same app server.

In that point you imagine the unique external configuration source you have in pure JEE* is JNDI.

[*] By "pure JEE" I mean without using external configuration servers like Spring Cloud Config (https://cloud.spring.io/spring-cloud-config/).

That said, the Weblogic deployment plans give you the easiest and comfortable way to load the JNDI attributes for each deployment and Spring Boot will populate the Spring Environment from JNDI.

The solution is really easy:

  1. Deploy your application and the Weblogic deployment plan customized for your environment (dev, pre, pro, ...)
  2. Use JNDI attributes values in your application as any other Spring Environment property: ${jndi.var} inside your application.properties, @Value(${jndi.var}), etc.
    • Note that Spring Boot will populate the Spring Environment with the JNDI attributes, so you will be able to use them in the same way you use the properties inside the application.properties file.

Hope it helps!

like image 128
eruiz Avatar answered Sep 19 '22 04:09

eruiz


You can set the active profiles using application.properties like below:

# PROFILES
spring.profiles.active= # Comma-separated list of active profiles.

As you have got multiple properties files packaged within the WAR (not recommended, rather dynamic properties to be extracted outside war), you can use @PropertySource(value = "classpath:application.dev.properties") to target a specific properties file to be used by the application.

You can look at here

like image 38
developer Avatar answered Sep 21 '22 04:09

developer