I have an existing bean overrideBean
defined in spring.xml
which I would like to override using annotations. I have tried the following to override the 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 above code the bean from the spring.xml
config is always instantiated and returned by the context.getBean
call.
The bean can be overridden by including another XML config file in the @ImportResource
however I would prefer to find a solution using annotations would be cleaner.
I'm working in an older app (spring 3.1.1) that's configured via xml, but I needed to shuffle some config around for testing without deviating too far from the production config. My approach is to use a 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;
}
}
Usually xml-registered beans have precedence. So you can override annotated beans with xml-configured ones but you are trying to do it the other way around. Can you not use a different bean name and select it among multiple candidates by using the @Qualifier annotation?
Most of the time combining xml with autoscanning is error-prone.
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