How can I access 'spring.application.name' when defined in bootstrap.properties?

Israel Fernández :

I have the following spring-boot 1.4.2.RELEASE sample app

@SpringBootApplication
public class Application {

    @Value("${spring.application.name}")
    private String applicationName;

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

And I have the following configuration defined in bootstrap.properties:

spring.application.name=sample-app

When run it I get the following error:

java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.application.name' in string value "${spring.application.name}"

Any hint on why it fails to inject 'spring.application.name'? Need to define it there to support other spring boot cloud.

DannielWhatever :

The first answer is correct. The default properties file is application.properties or application.yml.

The bootstrap file is properly for Spring Cloud.

See http://projects.spring.io/spring-cloud/spring-cloud.html#_the_bootstrap_application_context

If you are using spring cloud, and the bootstrap file is not working, you need to enable the "cloud" Spring profile.

For example using:

./gradlew -Dspring.profiles.active=cloud bootrun 

or

./mvnw spring-boot:run -Dspring.profiles.active=cloud

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=453230&siteId=1
Recommended