I am using spring 3.1 profiles and want would like Spring to print out on startup what profiles are active. For example are the first few lines of the output in the log file.
02:59:43,451 INFO [ContextLoader] Root WebApplicationContext: initialization started
02:59:43,544 INFO [XmlWebApplicationContext] Refreshing Root WebApplicationContext: startup date [Sun Dec 30 02:59:43 EST 2012]; root of context hierarchy
02:59:43,610 INFO [XmlBeanDefinitionReader] Loading XML bean definitions from class path resource [spring.xml]
02:59:43,835 INFO [XmlBeanDefinitionReader] Loading XML bean definitions from class path resource [spring-security.xml]
02:59:43,971 INFO [SpringSecurityCoreVersion] You are running with Spring Security Core 3.1.3.RELEASE
02:59:43,971 INFO [SecurityNamespaceHandler] Spring Security 'config' module version is 3.1.3.RELEASE
What I want to see from spring is something that prints out the version of spring in use along with which profiles are currently active.
How can I get spring to printout it's version and what profiles are active?
Spring profiles can also be activated via Maven profiles, by specifying the spring. profiles. active configuration property. This command will package the application for prod profile.
The solution would be to create more property files and add the "profile" name as the suffix and configure Spring Boot to pick the appropriate properties based on the profile. Then, we need to create three application. properties : application-dev.
Implement EnvironmentAware
interface
For example
class MyEnvironmentAware implements EnvironmentAware{
private static Environment env = null;
@Override
public void setEnvironment(Environment environment) {
env = environment;
//log the stuff you want here
}
}
Mark this class as Spring bean
or
just inject Environment
in one of your eagerly loading bean and print the detail you need from it
like
@Autowired
Environment env;
in your eagerly loading bean, and print it
You could get it by configuring log4j, as the Environment
object logs the activating of the profile at DEBUG
level.
log4j.logger.org.springframework.core.env=DEBUG, A1
If A1
is your log appender. Unfortunately, there is a lot of other stuff coming at DEBUG level, so it is not really nice, but you get the active profile without source modification.
The config logs in a standalone Swing app on startup:
58 [main] DEBUG org.springframework.core.env.StandardEnvironment - Activating profile 'production'
Note, this is fragile, as it relies on debug level logs, which can change rapidly in every commit to spring.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With