Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 796
Israel Fernández Avatar asked Dec 15 '16 01:12

Israel Fernández


People also ask

Where is spring application name defined?

It is located inside the src/main/resources folder, as shown in the following figure. Spring Boot provides various properties that can be configured in the application. properties file. The properties have default values. We can set a property(s) for the Spring Boot application.

How do I get a list of spring Boot application properties?

To see all properties in your Spring Boot application, enable the Actuator endpoint called env . This enables an HTTP endpoint which shows all the properties of your application's environment. You can do this by setting the property management.

How do you put an application name on spring Boot?

Use of @Value Annotation Note − If the property is not found while running the application, Spring Boot throws the Illegal Argument exception as Could not resolve placeholder 'spring.application.name' in value "${spring.application.name}".

Does bootstrap override application properties?

Overview. Spring Boot is an opinionated framework. Despite this, we usually end up overriding autoconfigured properties in an application configuration file such as application.


2 Answers

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
like image 137
DannielWhatever Avatar answered Oct 19 '22 00:10

DannielWhatever


By default, if you don't specify any properties source, spring boot will lookup for your property in the file application.properties. Therefore, you should rename your property file to that default name or manually specify a properties source for your bootstrap.properties

like image 5
Duy Nguyen Avatar answered Oct 19 '22 00:10

Duy Nguyen