2016-11-25 162 views
0

我有以下的配置類的web應用程序添加豆和攔截到的應用程序上下文:從外部罐子

@Configuration 
@EnableWebMvc 
@EnableSpringDataWebSupport 
class CustomMvcConfiguration extends WebMvcConfigurerAdapter { 

    @Bean 
    public MyBean myBean() { 
     return new MyBean(); 
    } 

    @Override 
    public void addInterceptors(InterceptorRegistry registry) { 
     registry.addInterceptor(new LocaleChangeInterceptor()); 
    } 
} 

我要添加應用一個罐子的依賴,增加另一個豆和其他攔截到context.In另一個項目我有另一個WebMvcConfigurerAdapter類,但是它不運行:

@Configuration 
@EnableWebMvc 
@EnableSpringDataWebSupport 
class OtherCustomMvcConfiguration extends WebMvcConfigurerAdapter { 

    @Bean 
    public OtherBean otherBean() { 
     return new OtherBean(); 
    } 

    @Override 
    public void addInterceptors(InterceptorRegistry registry) { 
     registry.addInterceptor(new CustomInterceptor()); 
    } 
} 

如果我嘗試了OtherBean注入一類的應用程序網絡的上下文中不存在:

@Inject 
private OtherBean otherBean; 

而CustomInterceptor不運行。如何將bean和攔截器從外部模塊添加到應用程序中?

回答

0

我認爲你只需要添加適當的@Include註釋到你的MVC配置。

@Include(OtherCustomMvcConfiguration.class)  
class CustomMvcConfiguration extends WebMvcConfigurerAdapter { 
... 
} 
+0

我不想修改我CustomMvcConfiguration,我想豆子添加到上下文只包括在pom.xml文件 – oscar

+0

的依賴。然後,你需要在加上'@ ComponentScan'註釋你的地方項目並指定'OtherCustomMvcConfiguration'類的包/類名 – nazlo