58

I've defined an application name using the bootstrap.yml file in my spring boot application.

spring:
  application:
    name: abc

How can i get this application name during runtime/programmatically ?

6 Answers 6

64

You should be able to use the @Value annotation to access any property you set in a properties/YAML file:

@Value("${spring.application.name}")
private String appName;
5
  • 4
    This works for application.yml/application.properties, but doesn't work for bootstrap.yml. Is there anything that needs to be configured? some annotation, somewhere?
    – Mubin
    Commented Jan 24, 2017 at 14:24
  • You can override spring.config.name to "bootstrap" (defaults to "application") or go further by specifying a different configuration explicitly using spring.config.location. Commented Mar 5, 2017 at 12:25
  • Value annotation is no longer recommended
    – loesak
    Commented May 14, 2020 at 3:00
  • 1
    this literally does not answer the question. OP clearly stated "bootstrap.yaml", which this fails to address.
    – Eugene
    Commented Jun 23, 2021 at 14:55
  • ok, but why if you don't explicit set the property the value is null ?? Commented Oct 27, 2022 at 14:59
28
@Autowired
private ApplicationContext applicationContext;    
...
this.applicationContext.getId();

Please, find this:

# IDENTITY (ContextIdApplicationContextInitializer)
spring.application.name=
spring.application.index=

In Spring Boot Reference Manual.

And follow with source code for that ContextIdApplicationContextInitializer class:

@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
    applicationContext.setId(getApplicationId(applicationContext.getEnvironment()));
}

Where the default behavior is with this:

/**
 * Placeholder pattern to resolve for application name
 */
private static final String NAME_PATTERN = "${vcap.application.name:${spring.application.name:${spring.config.name:application}}}";
3
  • The question asks for the application name, not the application ID. Commented Jan 22, 2019 at 5:38
  • 2
    The application ID is the application name. For me, the getApplicationName() returns nothing and getApplicationId() returns the name of the application, as set with the spring.application.name property.
    – Jan Bodnar
    Commented Jan 22, 2020 at 9:16
  • getId() returns a combination of the value of spring.application.name and spring.application.index. if no index value is set, it is defaulted. in my case calling getId() returns my-application-1 when the value of spring.application.name is my-application
    – loesak
    Commented May 17, 2020 at 2:18
7

Since the @Value annotation is discouraged in Spring Boot when referencing configuration properties, and because applicationContext.getId(); doesn't always return the value of spring.application.name another way is to get the value from the Environment directly

private final Environment environment;

...

public MyBean(final Environment environment) {
    this.environment = environment;
}

...

private getApplicationName() {
    return this.environment.get("spring.application.name");
}

Another possible way would be to create your own ConfigurationProperties class to get access to the value.

I'm not saying these are the best ways, and I hope/wish that there is a better way, but it is a way.

6
  • do you have an authoritative source that the value annotation is discouraged? The Spring Boot documentation does not seem to support that: docs.spring.io/spring-boot/docs/current/reference/html/…
    – dschulten
    Commented Apr 19, 2021 at 5:59
  • 2
    I found this: "Using the @Value("${property}") annotation to inject configuration properties can sometimes be cumbersome, especially if you are working with multiple properties or your data is hierarchical in nature" and a discussion of differences between @Value and @ConfigurationProperties docs.spring.io/spring-boot/docs/2.4.x/reference/htmlsingle/….
    – dschulten
    Commented Apr 23, 2021 at 5:03
  • I don't have an authoritative source. I did read it in Springs own documentation at some point, but maybe that's since changed. The idea is that configuration, when using Spring Boot, is best to come from the use of configuration properties so that the application can validate the configuration and fail on startup if any validation failed.
    – loesak
    Commented Jun 24, 2021 at 16:52
  • The issue I've seen in the real world is that developers will use the @Value annotation to access a property by name directly. If that property is not set or not of the expected type, the application can fail while performing runtime operations. It also makes finding usage of such configuration harder.
    – loesak
    Commented Jun 24, 2021 at 16:52
  • 1
    I find it much safer to define all configuration in configuration classes an access the properties from them as injected beans. Accessing from the class directly isn't always obvious in some cases like where an annotation is required. In these cases, because it's SPEL, you can access the configuration property from the bean using the SPEL syntax #{myConfigProps.myConfig}.
    – loesak
    Commented Jun 24, 2021 at 16:52
2

Note! If your using a SpringBootTest, you need to suplly the properties/yml. Otherwise, the environment/appcontext does not load the config files. The, your app name is not set. Like so:

@PropertySource("classpath:application.properties")
@RunWith(SpringRunner.class)
@SpringBootTest
....
1

This post is aged but I hate unanswered questions. So use the following snippet:

@Value("${spring.application.name [: defaultValue]}") 
private String appName;

What is between [] is optional.

0

So I found a really ugly way to do this, but it works so I'm not searching further. Maybe this will help someone.

The basic premise is that spring Environment stores the value inside a propertySource.. It appears that bootstrap config is stored in the ResourcePropertySource and so you can get it from that. For me it is currently throwing an exception, but then I can get the value out of the exception, so I haven't looked any further:

    try {
        this.environment.getProperty("name", ResourcePropertySource.class);
    } catch (ConversionFailedException e) {
        String res = (String)e.getValue();
    }

And then you can just do this for every property you are interested in.

Like I said ugly, but it works.

Not the answer you're looking for? Browse other questions tagged or ask your own question.