Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create spring properties by condition?

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?

like image 252
membersound Avatar asked Feb 27 '15 14:02

membersound


People also ask

How create properties file in spring?

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.

What does @configuration do in spring?

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.

How do I set Environment specific properties in spring boot?

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.

How do you make conditional beans in spring?

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.


1 Answers

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

like image 103
dunni Avatar answered Oct 22 '22 14:10

dunni