user1575601
user1575601

Reputation: 425

Unable to read application.properties in spring boot application

I am unable to read application.properties value in my project in classes that are injected as a bean from another project. My project is using another project which has classes which needs to read configuration from application.properties. Mine is a maven project and a spring boot application having application.properties in src/main/resources folder and those properties values are defined in it. What is it that I am missing that it is unable to read file values? Or is there is any other mechanism for setting these properties for classes loaded via component-scan

Below line of code works fine, it is able to read value from the application.properties: @PostConstruct void init() throws ClassNotFoundException, IOException { System.out.println(context.getEnvironment().getProperty("env.host", "default value")); }

Now there is another project which I am using in mine. When loading the classes those beans are getting initialized as dependency of another class, there also it tries to read same value in constants file as

   static final String HOST_PROPERTY = "${env.host}";

There this value is not getting initialized to value from applictaion.properties

Upvotes: 3

Views: 3794

Answers (1)

gokhanbirincii
gokhanbirincii

Reputation: 585

If you want to get application.properties values,you have to two option. 1) you can autowire Environment class and use its getProperty() method as

@Autowired
Environment env;
env.getProperty("${env.host}");

2)@Value annotation

@Value("${env.host}")
    String HOST_PROPERTY;

Upvotes: 1

Related Questions