Is it possible to create a application.properties
property by condition?
I want to define a filename that should be "default_name_test" if spring.profiles.active=test
, and "default_name" otherwise.
Pseudeocode:
#application.properties:
filename=default_name${spring.profiles.active} = test ? "_test" : "";
Is that possible at all?
properties in default location. Spring Boot loads the application. properties file automatically from the project classpath. All you have to do is to create a new file under the src/main/resources directory.
Spring @Configuration annotation is part of the spring core framework. Spring Configuration annotation indicates that the class has @Bean definition methods. So Spring container can process the class and generate Spring Beans to be used in the application.
Environment-Specific Properties File. If we need to target different environments, there's a built-in mechanism for that in Boot. We can simply define an application-environment. properties file in the src/main/resources directory, and then set a Spring profile with the same environment name.
Another way of making the component conditional would be to place the condition directly on the component class: @Service @Conditional(IsDevEnvCondition. class) class LoggingService { // ... } We can apply the above example to any bean declared with the @Component, @Service, @Repository, or @Controller annotations.
As Dave Syer already mentioned in his comment, it is possible to use profile specific property files to configure the application with Spring Boot. So in your application.properties
you define the property with its default value, and in the file application-test.properties
you define the property with default_name_test. The name pattern for the config files is application-${profilename}.properties
.
See the Spring Boot manual here: http://docs.spring.io/spring-boot/docs/1.2.2.RELEASE/reference/htmlsingle/#boot-features-external-config-profile-specific-properties
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