Spring Boot get spring.profiles.active value in a static block
02:49 03 Dec 2020

Is it possible to get the value of the spring active profile in some class's static block?

I have tried @value("$(spring.profiles.active)") and @Autowired Environment env; to get the value but both resulted as null inside the static block.

I understand that the static block is executed before spring initialization during bean loading, So is there any workaround to get active profile value or any value from application.yml inside the static block?

Sample Code:

@Value("$(spring.profiles.active)")
private String env;

static {
        URL url = null;
        WebServiceException e = null;
        try {
            ClassPathResource wsdlLoc = new ClassPathResource("/wsdl/Transaction_"+env+".wsdl");
            url = new URL(wsdlLoc.getURL().toString());
        } catch (IOException ex) {
            e = new WebServiceException(ex);
        }
        TRANSACTIONPROCESSOR_WSDL_LOCATION = url;
        TRANSACTIONPROCESSOR_EXCEPTION = e;
    }
java spring spring-boot properties