@Bean 控制器没有被@EnableMVC 接收
@Bean Controller not getting picked up by @EnableMVC
我的大部分@Controller 都是通过组件扫描获取的。但是,有一些,例如我在 Spring Social 中使用的那些,被创建为 @Beans。我刚刚从主要 xml 迁移到仅 JavaConfig 并升级到 Spring 4.1.9.
但是,创建为 @Beans 的控制器端点正在创建 404。
有什么想法吗?
package nl.project.webapp.config;
@Order(1)
public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer{
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[]{WebAppConfig.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[]{ServletConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
[...]
}
package nl.project.webapp.config;
@Configuration
@ComponentScan(basePackages = {"nl.project.webapp"},excludeFilters={
@ComponentScan.Filter(type=FilterType.ANNOTATION,value=Controller.class),
@ComponentScan.Filter(type=FilterType.ANNOTATION,value=RestController.class)
})
@Import({AppConfig.class,JPAConfig.class})
@PropertySource("classpath:msa.properties")
public class WebAppConfig {
[...]
}
package nl.project.webapp.config;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"nl.project.webapp.controller"},includeFilters={
@ComponentScan.Filter(type=FilterType.ANNOTATION,value=Controller.class),
@ComponentScan.Filter(type=FilterType.ANNOTATION,value=RestController.class)
})
public class ServletConfig extends WebMvcConfigurerAdapter{
[...]
}
package nl.project.webapp.social.config;
@Configuration
public class SocialConfig{
@Bean
public MyConnectController connectController(MessageSource messages, UsorManager userMgr, PhotoManager photoMgr) {
MyConnectController connectController = new MyConnectController(connectionFactoryLocator, connectionRepository);
connectController.setConnectInterceptors(Arrays.asList(new ConnectInterceptor<?>[]{
new TwitterConnectInterceptor(userMgr, photoMgr, messages),
new FacebookConnectInterceptor(userMgr, photoMgr, messages),
new LinkedInConnectInterceptor(userMgr, photoMgr, messages),
new GoogleConnectInterceptor(userMgr, photoMgr, messages),
}));
return connectController;
}
@Bean
public MySignInController signinController(MessageSource messages, UsorManager userMgr, PhotoManager photoMgr){
MySignInController signinController = new MySignInController(connectionFactoryLocator, usersConnectionRepository, new SimpleSigninAdapter(userMgr));
signinController.setSignInInterceptors(Arrays.asList(new ProviderSignInInterceptor<?>[]{
new FacebookSigninInterceptor(userMgr, photoMgr, messages),
new LinkedInSigninInterceptor(userMgr, photoMgr),
new GoogleSigninInterceptor(userMgr, photoMgr)
}));
return signinController;
}
}
package nl.project.webapp.social.controller;
@Controller
@RequestMapping("/signin")
public class MySignInController extends ProviderSignInController {
public MySignInController(
ConnectionFactoryLocator connectionFactoryLocator,
UsersConnectionRepository usersConnectionRepository,
SignInAdapter signInAdapter) {
super(connectionFactoryLocator, usersConnectionRepository, signInAdapter);
this.connectionFactoryLocator = connectionFactoryLocator;
}
[...]
问题是由 WebAppConfig 而不是 ServletConfig 导入的 SocialConfig 引起的。尽管文档表明上下文中可用的任何 Controller bean 都将通过使用“@EnableWebMVC”来获取,但尚不清楚这不适用于通过 webapp 上下文加载的 Controller bean。
我的大部分@Controller 都是通过组件扫描获取的。但是,有一些,例如我在 Spring Social 中使用的那些,被创建为 @Beans。我刚刚从主要 xml 迁移到仅 JavaConfig 并升级到 Spring 4.1.9.
但是,创建为 @Beans 的控制器端点正在创建 404。
有什么想法吗?
package nl.project.webapp.config;
@Order(1)
public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer{
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[]{WebAppConfig.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[]{ServletConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
[...]
}
package nl.project.webapp.config;
@Configuration
@ComponentScan(basePackages = {"nl.project.webapp"},excludeFilters={
@ComponentScan.Filter(type=FilterType.ANNOTATION,value=Controller.class),
@ComponentScan.Filter(type=FilterType.ANNOTATION,value=RestController.class)
})
@Import({AppConfig.class,JPAConfig.class})
@PropertySource("classpath:msa.properties")
public class WebAppConfig {
[...]
}
package nl.project.webapp.config;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"nl.project.webapp.controller"},includeFilters={
@ComponentScan.Filter(type=FilterType.ANNOTATION,value=Controller.class),
@ComponentScan.Filter(type=FilterType.ANNOTATION,value=RestController.class)
})
public class ServletConfig extends WebMvcConfigurerAdapter{
[...]
}
package nl.project.webapp.social.config;
@Configuration
public class SocialConfig{
@Bean
public MyConnectController connectController(MessageSource messages, UsorManager userMgr, PhotoManager photoMgr) {
MyConnectController connectController = new MyConnectController(connectionFactoryLocator, connectionRepository);
connectController.setConnectInterceptors(Arrays.asList(new ConnectInterceptor<?>[]{
new TwitterConnectInterceptor(userMgr, photoMgr, messages),
new FacebookConnectInterceptor(userMgr, photoMgr, messages),
new LinkedInConnectInterceptor(userMgr, photoMgr, messages),
new GoogleConnectInterceptor(userMgr, photoMgr, messages),
}));
return connectController;
}
@Bean
public MySignInController signinController(MessageSource messages, UsorManager userMgr, PhotoManager photoMgr){
MySignInController signinController = new MySignInController(connectionFactoryLocator, usersConnectionRepository, new SimpleSigninAdapter(userMgr));
signinController.setSignInInterceptors(Arrays.asList(new ProviderSignInInterceptor<?>[]{
new FacebookSigninInterceptor(userMgr, photoMgr, messages),
new LinkedInSigninInterceptor(userMgr, photoMgr),
new GoogleSigninInterceptor(userMgr, photoMgr)
}));
return signinController;
}
}
package nl.project.webapp.social.controller;
@Controller
@RequestMapping("/signin")
public class MySignInController extends ProviderSignInController {
public MySignInController(
ConnectionFactoryLocator connectionFactoryLocator,
UsersConnectionRepository usersConnectionRepository,
SignInAdapter signInAdapter) {
super(connectionFactoryLocator, usersConnectionRepository, signInAdapter);
this.connectionFactoryLocator = connectionFactoryLocator;
}
[...]
问题是由 WebAppConfig 而不是 ServletConfig 导入的 SocialConfig 引起的。尽管文档表明上下文中可用的任何 Controller bean 都将通过使用“@EnableWebMVC”来获取,但尚不清楚这不适用于通过 webapp 上下文加载的 Controller bean。