Set the property based on condition in spring xml?

Set the property based on condition in spring xml?

spring bean @conditional on property xml
spring conditional bean
spring xml import based on profile
environment specific spring xml
spring import resource based on property
spring bean @conditional on profile
spring autowire based on property

I want to include the file under spring.xml based on condition like

<bean id="ehcache" class="com.MyCustomBean" scope="singleton">
           <property name="fileLoc" value="file_dev.xml" />
           <property name="fileLoc" value="file_prod.xml" />


where environment is property defined under property file.

For information, I am already using spring PropertySourcesPlaceholderConfigurer to resolves ${...} placeholders within bean definition property values .

You can consider to use the spring profile, you can divide the xml files to different config files please refer to

Conditionally load beans in Spring using XML configuration, Also, we don't want Spring to fail when the SYS_ENV_VAR property isn't set. Hence, we set both “ignoreUnresolvablePlaceholders" and  In XML, new properties files can be made accessible to Spring via the <context:property-placeholder … > namespace element: <context:property-placeholder location="" /> The file should be placed under /src/main/resources so that it will be available on the classpath at runtime.

You should do

<property name="name" value="#{ ${environment}== dev ? file_dev.xml : file_prod.xml }"/>

Conditional Beans with Spring Boot, Spring Boot Conditional beans tutorial shows how to register beans into the Spring Boot application context based on conditions. If a @Configuration class is marked with @Conditional , all of the @Bean methods, @Import annotations, is registered only if there is a welcomebean.enabled property set to  You should note the difference in Beans.xml file defined in the constructor-based injection and the setter-based injection. The only difference is inside the <bean> element where we have used <constructor-arg> tags for constructor-based injection and <property> tags for setter-based injection.

I had similar case, but I needed to inject different beans depending on property: "". In my case this solution was successful

 <property name="name" ref="#{ ${}==true ? 'bean1' : 'bean2' }"/>

Spring Boot Conditional beans, This page shows how to load property file from the classpath using xml based configuration. You can specify multiple property file seperated by comma:  Spring bean java based configuration using @Configuration and @Bean. In the previous example we have seen how to configure a bean in the xml based configuration file. In spring 3, we got another alternative solution, we can move all xml based configurations to java based configurations.

A workaround solution:
<import resource="file_${env:dev}.xml"/>

Read property file in spring using xml based configuration file , You can declare a bean definition either using XML based The above example shows how to configure conditional on property annotation. Is it possible to set a property value in Ant property files (as opposed to build.xml) in a conditional way? For example, if apache.root property is set - the my_property will be ${apache.root}/myapp , /var/www/myapp otherwise.

Conditional Beans Creation In Spring Boot, Spring's Java-based Configuration; Spring Profiles. Please go through “Spring Conditions based on some or all Bean properties values. Conditions based on some parameter values. This list is just a set of examples. Has anyone tried to auto-wire different beans into a Spring-managed bean based on a condition? For e.g. if some condition is met, inject class A, else B? I saw in one of the Google search results that it is possible with SpEL (Spring Expression Language), but could not locate a working example.

Spring @Conditional Annotation, Bean configuration may also depend on what version of Java is available, the value of a system property/environmental variable, or whether your  Since the convenience of defining property sources with annotations was introduced in the recently released Spring 3.1, XML based configuration was necessary in the previous versions.

Spring 4 - Conditional Bean Configuration, In a previous post, I made a small demo on how to load Java beans based on properties defined in a configuration file. While this is a very  In this quick tutorial, we illustrated examples of how we can inject dependency using the XML-based configuration using Spring Framework. The implementation of these examples can be found in the GitHub project – this is a Maven-based project, so it should be easy to import and run as is.