yathirigan
yathirigan

Reputation: 6059

How to access a Spring Boot Application's name programmatically?

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 ?

Upvotes: 59

Views: 91287

Answers (6)

Richard
Richard

Reputation: 140

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.

Upvotes: 0

Meziane
Meziane

Reputation: 1667

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.

Upvotes: 1

loesak
loesak

Reputation: 1613

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.

Upvotes: 8

Roland Roos
Roland Roos

Reputation: 1083

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
....

Upvotes: 2

Daniel Cottone
Daniel Cottone

Reputation: 4480

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;

Upvotes: 65

Artem Bilan
Artem Bilan

Reputation: 121272

@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}}}";

Upvotes: 29

Related Questions