如何使用Annotations覆盖xml中定义的spring bean?

时间:2013-08-23 10:43:33

标签: spring spring-annotations

我在overrideBean中定义了一个现有的bean spring.xml,我想用注释覆盖它。我已经尝试过以下方法来覆盖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;
    }

}

使用上面的代码,spring.xml配置中的bean始终被实例化并由context.getBean调用返回。

可以通过在@ImportResource中包含另一个XML配置文件来覆盖bean,但是我更愿意找到使用注释的解决方案。

2 个答案:

答案 0 :(得分:4)

我正在使用通过xml配置的旧应用程序(春季3.1.1),但我需要将一些配置随机播放以进行测试,而不会偏离生产配置太远。我的方法是使用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;
    }
}

答案 1 :(得分:3)

通常xml注册的bean具有优先权。因此,您可以使用xml配置的bean覆盖带注释的bean,但是您尝试以相反的方式执行此操作。您是否可以使用不同的bean名称并使用 @Qualifier 注释在多个候选项中选择它?

大多数时候将xml与自动扫描相结合容易出错。