chiperortiz
chiperortiz

Reputation: 4991

Java Spring accesing property from application.properties

I have a entry on application.properties like this.

stackoverflow.questiontitle=I cant see this from environment

But in my code I'm unable to see it.

enter image description here

As you can see the @Value is working but this time I need it from Environment or ApplicationContext. How can I do that?

Update

I mean the @Value I can see the property but I need it from Environment on other class, yes it is failing on runtime.

Is running prod profile.

Upvotes: 0

Views: 85

Answers (1)

Dirk Deyne
Dirk Deyne

Reputation: 6936

This should work...

import org.springframework.core.env.Environment;

@Component
class WithEnvironment {

    WithEnvironment(Environment environment) {
        System.out.println(environment.getProperty("stackoverflow.questiontitle"));
    }
}

To see the value in IntelliJ IDEA in debug mode you can use:

  • ((ApplicationEnvironment)environment).getProperty("stackoverflow.questiontitle")

  • add a New Class Level Watch... on environment with getProperty("stackoverflow.questiontitle")


enter image description here

Upvotes: 1

Related Questions