How to assign different value to a spring bean variable from yml file based on spring profile

53 Views Asked by At

I have a spring java class like below

@Getter
@Setter
@ConfigurationProperties(prefix = "check.connection")
public class MyConnection{
    private String url;
    private String key;
}

Below is my content of my default yml file (containing url and key),

check.connection.url = www.test.com
check.connection.key = DEFAULTKEY
check.connection.key.integration = INTEGRATIONENVKEY

Problem Statement : I want to map 'key' in my java class with 'check.connection.key.integration' when application is running on integration env. and in all other envs, it should be mapped to 'check.connection.key'. I know I have to use @Profile and @value somehow in MyConnection.java file, but not able to identify exact syntax.

Based on above code (@ConfigurationProperties), in all env,'key' is getting mapped to 'check.connection.key', which I dont want for Integration environment.

2

There are 2 best solutions below

0
Harry Coder On

The @ConfigurationProperties annotation is used to map a class properties with a configuration file properties. Here you don't need to call @Value because Spring will automatically configure MyConnection to map with your properties in the application.propertie file.

There are some modifications you have to do in your class:

@Getter
@Setter
@Configuration
@ConfigurationProperties(prefix = "check.connection")
public class MyConnection{
    private String url;
    private String key;
    private String keyIntegration;
}
  1. Add @Configuration on your class
  2. Add a new propertie keyIntegration to map it with your file propertie check.connection.key.integration

For more details, check this link.

0
NewUser On

Spring provides this capability out of the box, if you need to manage different values for different profiles, then you can create application-<profile-name).yml (or properties) and spring will override the values provided when a profile is activated.

You should not be creating different keys for your profiles. That is an anti pattern.

Here is a step-by-step guide for a minimal POC.

Create your main application:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
import org.springframework.boot.context.properties.EnableConfigurationProperties;

@SpringBootApplication
@EnableConfigurationProperties
@ConfigurationPropertiesScan(basePackageClasses = Connection.class)
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Pay attention to basePackageClasses = Connection.class and read more about it in spring docs.

Here is how the Connection class looks in my project.

import jakarta.annotation.PostConstruct;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConfigurationProperties(prefix = "check.connection")
public class Connection {
    private String url;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    @PostConstruct
    public void printAll() {
        System.out.println(this.url);
    }
}

I have two files in resouces application.yml, application-it.yml.

This is the result when I run the app in test profile

2023-06-29T09:58:29.036+05:30  INFO 6153 --- [           main] c.e.kotlinspringexperiments.Application  : Starting Application using Java 17.0.6 with PID 6153 (....)
2023-06-29T09:58:29.038+05:30  INFO 6153 --- [           main] c.e.kotlinspringexperiments.Application  : The following 1 profile is active: "test"
test
2023-06-29T09:58:29.658+05:30  INFO 6153 --- [           main] o.s.b.web.embedded.netty.NettyWebServer  : Netty started on port 8080
2023-06-29T09:58:29.664+05:30  INFO 6153 --- [           main] c.e.kotlinspringexperiments.Application  : Started Application in 0.794 seconds (process running for 6.043)

and this is when I run without any profile.

2023-06-29T10:01:09.773+05:30  INFO 6362 --- [           main] c.e.kotlinspringexperiments.Application  : Starting Application using Java 17.0.6 with PID 6362 (...)
2023-06-29T10:01:09.775+05:30  INFO 6362 --- [           main] c.e.kotlinspringexperiments.Application  : No active profile set, falling back to 1 default profile: "default"
default
2023-06-29T10:01:10.421+05:30  INFO 6362 --- [           main] o.s.b.web.embedded.netty.NettyWebServer  : Netty started on port 8080
2023-06-29T10:01:10.426+05:30  INFO 6362 --- [           main] c.e.kotlinspringexperiments.Application  : Started Application in 0.821 seconds (process running for 6.092)