Skip to content
Advertisement

How can i fetch property from application-test.properties file in Java class?

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”

JavaScript

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

JavaScript

PropertiesConfig class:

JavaScript

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

JavaScript

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

Advertisement

Answer

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.

JavaScript

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

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement