2017-05-19 26 views
0

我有2個應用程序上下文:合併AnnotationConfigWebApplicationContext與ClassPathXmlApplicationContext的

AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext(); 
    appContext.register(BeansConfig.class); 

ClassPathXmlApplicationContext secContext = new ClassPathXmlApplicationContext("/META-INF/spring-security.xml"); 

我怎樣才能讓這些成一個單一的情況下,無需重新編寫帶有註解的XML或反之亦然?

ApplicationContext mergedContext = // appContext.merge(secContext) ???? 

是這樣的可能嗎?

+0

認沽'@ImportResource(添加這種「類路徑」 /META-INF/spring-security.xml「)''在你的'BeansConfig'上......但是你爲什麼要創建自己的配置呢?通常這是你不應該做的事情,並且表明你正在做錯誤的事情。(該配置應該由'ContextLoaderLIstener'或'DispatcherServlet'加載)。 –

回答

0

使用@Configuration類導入XML和註釋配置

@Configuration 
@Import(BeansConfig.class) 
@ImportResource("classpath:/spring-security.xml") 
public class AppConfig{} 

您也可以直接在春季啓動主類,如果你使用Spring啓動