0

Hi I am little bit confusign about using profiles in spring.My Scenario is I have a custom properties file.And It is values changes for each environment(dev,test,prod).I use same bean for each environment.But I want to change values for each environments. In this property files all keys are same,only their values different.

mydev.properties 
mytest.properties
myprod.properties

So How should I implement profile logic to my code in my scenario(Bean is same ,values are different)

//Here is my bean
@Component
@PropertySource("my.properties")
@ConfigurationProperties(prefix = "my")
public class MyProperties
{
....

I will add to 'spring.profiles.active' to my propertysource and is this enough?

//I plan to add spring.profiles.active

 @Component
 @PropertySource("my${spring.profiles.active}.properties")
@ConfigurationProperties(prefix = "my")
public class MyProperties
{
....
Bilgehan
  • 1,135
  • 1
  • 14
  • 41

1 Answers1

1

Please go through the Spring Boot Reference : Section 2.4. Profile-specific Properties

In addition to application.properties files, profile-specific properties can also be defined by using the following naming convention: application-{profile}.properties.

One need to define the profile specific properties in application-{profile}.properties and declare the active profile

You can use a spring.profiles.active Environment property to specify which profiles are active.

To answer your concern , the property value for the current active profile will be wired to the bean. Also note that

Profile-specific properties are loaded from the same locations as standard application.properties, with profile-specific files always overriding the non-specific ones, whether or not the profile-specific files are inside or outside your packaged jar.

If several profiles are specified, a last-wins strategy applies. For example, profiles specified by the spring.profiles.active property are added after those configured through the SpringApplication API and therefore take precedence.

In your case , the ideal way to define profile specific properties would be

application-dev.properties

application-test.properties

application-prod.properties

R.G
  • 6,436
  • 3
  • 19
  • 28