2016-11-25 158 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