Home > front end >  How can i fetch property from application-test.proprties file in Java class?
How can i fetch property from application-test.proprties file in Java class?

Time:10-04

I have set the variable "spring.profiles.active" in my environment to "test" and I have below file in my src/main/resources.

application-test.properties

It has one property "machine"

machineName=mumbai

I want to access this property in one of my Java based class.

package com.test.service;

@Component
@RequiredArgsConstructor
public class TestMachine {

    @Value("${machineName}")
    private String machineName;

    @Override
    public void checkMachine() {
        System.out.println(machineName);
    }
}

PropertiesConfig class:

@Configuration
public class PropertiesUtils {

    public static void initProperties() {
        String activeProfile = System.getProperty("spring.profiles.active");
        if (activeProfile == null) {
            activeProfile = "test";
        }
        PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
        Resource[] resources = new ClassPathResource[] {new ClassPathResource("application.properties"),
                        new ClassPathResource("application-"   activeProfile   ".properties")};
        propertySourcesPlaceholderConfigurer.setLocations(resources);
    }
}

But while running this as a Spring boot application in Eclipse. I am getting below error:

Error creating bean with name 'TestMachine': Injection of autowired dependencies failed;

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'machineName' in value "${machineName}" at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:180) ~[spring-core-5.3.9.jar:5.3.9]

What am I missing? I found this way only in most of the websites.

CodePudding user response:

In Eclipse you have the set the spring.profiles.active environment variable in the Run Configurations.

Just go to Run -> Run Configurations... and select the tab Environment. Add the spring.profiles.active environment variable with the value of test.

enter image description here

CodePudding user response:

To give you a better answer please show your application.properties and the full stacktrace, but this may help.

You can get the current profile using the Environment class.

package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import org.springframework.core.env.Environment;

@SpringBootApplication
public class DemoApplication {

  @Autowired
  Environment env;

  public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
  }

  @Bean
  public CommandLineRunner run() {
    return new CommandLineRunner() {

      @Override
      public void run(String... args) {
        for (var  p: env.getActiveProfiles()) {
          System.out.println(p);
        }
      }

    };
  }
}

And pass the profile via cli

use this

mvn spring-bot:run -Dspring-boot.run.profiles=test

or

gradle bootRun --args "'--spring.profiles.active=test'"

or

java -jar *.jar -Dspring.profiles.active=test

  • Related