How do you redefine the spring bean defined in xml using annotations?

I have an existing bean overrideBean defined in spring.xml that I would like to override with annotations. I tried the following override bean:

 @Configuration @ImportResource({"/spring.xml"}) public class Main { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(DdwMain.class); Object o = context.getBean("overrideBean"); // o should be null but it is not } @Bean(name="overrideBean") public OverrideBean overrideBean() { return null; } } 

Using the code above, the bean from the spring.xml configuration spring.xml always created and returned by calling context.getBean .

The bean can be overridden by including another XML configuration file in @ImportResource , however I would prefer to find a solution using annotations would be cleaner.

+6
source share
2 answers

Usually xml-registered beans take precedence. This way you can override annotated beans with xml-tuned ones, but you are trying to do it the other way around. Can you use a different bean name and select it among several candidates using the @Qualifier annotation?

In most cases, combining xml with autoscan is error prone.

+3
source

I am working in an older application (spring 3.1.1) configured through xml, but I needed to shuffle some configuration for testing without deviating too far from the production configuration. My approach is to use BeanPostProcessor.

 package myapp.test; import javax.servlet.ServletContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.context.annotation.ImportResource; import org.springframework.mock.web.MockServletContext; import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer; import myapp.spring.config.ComplexSpringConfiguration; /** * Closely resembles production configuration for testing * @author jim */ @Configuration @ImportResource("file:src/main/webapp/WEB-INF/spring-servlet.xml") @Import(ComplexSpringConfiguration.class) public class TestConfig { final Logger logger = LoggerFactory.getLogger(getClass()); static { System.setProperty("env.test", "system"); } //Override templateLoaderPath with something amenable to testing @Bean public BeanPostProcessor beanPostProcessor(){ return new BeanPostProcessor(){ @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { //Override templateLoaderPath with something amenable to testing if(beanName.equals("freemarkerConfig")) { logger.debug("overriding bean with name:" + beanName); FreeMarkerConfigurer fc = new FreeMarkerConfigurer(); fc.setTemplateLoaderPath("file:src/main/webapp/WEB-INF/freemarker"); bean = fc; } return bean; } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { return bean; } }; } @Bean public ServletContext servletContext(){ MockServletContext mockContext = new MockServletContext(); mockContext.setContextPath("/myapp"); return mockContext; } } 
+4
source

Source: https://habr.com/ru/post/952354/


All Articles