Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to enable or disable the Spring bean definition in applicationContext.xml file?

Is there any way to enable or disable a java bean definition in application context?

<bean id="enBean" classs="com.en.bean.BeanName">
   <property name="prop1"/>
</bean>

Or, is there any way to load the bean conditionally defined in application context?

like image 436
user1065374 Avatar asked Nov 26 '11 14:11

user1065374


People also ask

What is required to load the beans configured in ApplicationContext XML file?

ClassPathXmlApplicationContext − This container loads the definitions of the beans from an XML file. Here you do not need to provide the full path of the XML file but you need to set CLASSPATH properly because this container will look like bean configuration XML file in CLASSPATH.

Can the configurations of Spring beans be defined in multiple XML files for one application context?

Yes, in large projects, having multiple Spring configurations increase maintainability and modularity. You can also upload one XML file that will contain all configs.

Which XML element is used to define bean in Spring configuration file?

In XMLbased configuration metadata, you use the id and/or name attributes to specify the bean identifier(s). This attribute specifies the scope of the objects created from a particular bean definition and it will be discussed in bean scopes chapter.

Which element will you use in Spring XML configuration file for enabling auto scanning of?

With Spring, we use the @ComponentScan annotation along with the @Configuration annotation to specify the packages that we want to be scanned. @ComponentScan without arguments tells Spring to scan the current package and all of its sub-packages.


1 Answers

There is a new feature @Profile in spring 3.1 that would do the job

From here

Spring 3.1 introduces the concept of environment profiles. A common use case is the setting up of beans that are different between development, QA and production environments. A typical example is going against a standalone DataSource in development versus looking up the DataSource from JNDI in production. Another example is a beans profile for profiling that can easily be turned on or off. You can add a profile attribute on a beans element in XML or add @Profile annotation in code. Note that a Spring bean can be assigned to multiple profiles.

<beans profile="dev">
    ...
</beans>
@Profile("dev")
public class Bean {
    ...
}

These profiles can be activated through the spring.profiles.active property which may be specified through an environment variable, a JVM system property, a Servlet in web.xml or JNDI. These profiles can also be activated through code using Environment.setActiveProfiles(String ...). To make bean profiles work, nested beans elements are now allowed in the Spring XML, although constrained only at the end of the file. Note that it's recommended to keep your bean topology as close as possible between environments, so your application gets properly tested across environments. You also use the Environment.containsProperty() method to search for properties across the different property sources. This property resolution also works for ${placeholder} variables in XML bean definitions.

like image 79
jmj Avatar answered Oct 05 '22 22:10

jmj