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 ?
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;
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
@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}}}";
getApplicationName()
returns nothing and getApplicationId()
returns the name of the application, as set with the spring.application.name
property.
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
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.
@Value
and @ConfigurationProperties
docs.spring.io/spring-boot/docs/2.4.x/reference/htmlsingle/….
Commented
Apr 23, 2021 at 5:03
@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.
#{myConfigProps.myConfig}
.
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
....
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.
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.