我在尝试复制一个简单的spring OAuth项目sparklr2时遇到了这个问题。源代码在这里 https://github.com/spring-projects/spring-security-oauth/tree/master/samples/oauth2/sparklr
源代码运行完美,当我用tomcat调试它时,它初始化WebMvcConfigurerAdapter中的所有@Bean,包括控制器。但是注意到没有使用@ComponentScan()。
然后我创建自己的MVC项目,复制几乎100%的代码,但我使用WebApplicationInitializer而不是AbstractDispatcherServletInitializer。我使用WebApllicationInitializer,因为我只学习了这种方法来编写MVC。
然后我运行项目,@ Bean初始化。然后我用我的浏览器检查/登录,得到404.这可能是因为春天不知道我有控制器,然后我将@ComponentScan添加到我的配置类,/ login现在显示。 但奇怪的是,所有与Controller相关的@Bean都没有初始化。所以,当我向那些控制器调用任何方法时,由于它们的属性没有被初始化,所以没有给我任何对象或null异常。
所以,我的观点是,该示例是如何工作的,我的意思是控制器和jsp正确处理和响应而不使用@ComponentScan? 从不同的角度来看,为什么@ComponentScan会阻止@Bean在我的项目中初始化?
我的WebApplicationInitializer
@Configuration
@EnableWebMvc
@ComponentScan("umedia.test.oauth.controller")
public class MvcConfig extends WebMvcConfigurerAdapter {
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
@Bean
public ContentNegotiatingViewResolver contentViewResolver()
throws Exception {
ContentNegotiationManagerFactoryBean contentNegotiationManager = new ContentNegotiationManagerFactoryBean();
contentNegotiationManager.addMediaType("json",
MediaType.APPLICATION_JSON);
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
MappingJackson2JsonView defaultView = new MappingJackson2JsonView();
defaultView.setExtractValueFromSingleKeyModel(true);
ContentNegotiatingViewResolver contentViewResolver = new ContentNegotiatingViewResolver();
contentViewResolver
.setContentNegotiationManager(contentNegotiationManager
.getObject());
contentViewResolver.setViewResolvers(Arrays
.<ViewResolver> asList(viewResolver));
contentViewResolver.setDefaultViews(Arrays.<View> asList(defaultView));
return contentViewResolver;
}
@Bean
public PhotoServiceImpl photoServices() {
List<PhotoInfo> photos = new ArrayList<PhotoInfo>();
photos.add(createPhoto("1", "marissa"));
photos.add(createPhoto("2", "paul"));
photos.add(createPhoto("3", "marissa"));
photos.add(createPhoto("4", "paul"));
photos.add(createPhoto("5", "marissa"));
photos.add(createPhoto("6", "paul"));
PhotoServiceImpl photoServices = new PhotoServiceImpl();
photoServices.setPhotos(photos);
return photoServices;
}
// N.B. the @Qualifier here should not be necessary (gh-298) but lots of
// users report needing it.
@Bean
public AdminController adminController(
TokenStore tokenStore,
@Qualifier("consumerTokenServices") ConsumerTokenServices tokenServices,
SparklrUserApprovalHandler userApprovalHandler) {
AdminController adminController = new AdminController();
adminController.setTokenStore(tokenStore);
adminController.setTokenServices(tokenServices);
adminController.setUserApprovalHandler(userApprovalHandler);
return adminController;
}
// this url, do I need to change it?
private PhotoInfo createPhoto(String id, String userId) {
PhotoInfo photo = new PhotoInfo();
photo.setId(id);
photo.setName("photo" + id + ".jpg");
photo.setUserId(userId);
photo.setResourceURL("/org/springframework/security/oauth/examples/sparklr/impl/resources/"
+ photo.getName());
return photo;
}
@Override
public void configureDefaultServletHandling(
DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Bean
public PhotoServiceUserController photoServiceUserController(
PhotoService photoService) {
PhotoServiceUserController photoServiceUserController = new PhotoServiceUserController();
return photoServiceUserController;
}
@Bean
public PhotoController photoController(PhotoService photoService) {
PhotoController photoController = new PhotoController();
photoController.setPhotoService(photoService);
return photoController;
}
@Bean
public AccessConfirmationController accessConfirmationController(
ClientDetailsService clientDetailsService,
ApprovalStore approvalStore) {
AccessConfirmationController accessConfirmationController = new AccessConfirmationController();
accessConfirmationController
.setClientDetailsService(clientDetailsService);
accessConfirmationController.setApprovalStore(approvalStore);
return accessConfirmationController;
}
/* @Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations(
"/resources/");
}
}
答案 0 :(得分:0)
所以,你有@ComponentScan
与控制器上的@Controller
进行互动+仍然会创建@Bean
吗?
第一步尝试删除@Beans
并尝试使用@Autowired
在控制器的构造函数上注入依赖项。然后@ComponentScan应该识别@Controller
,注入依赖关系并使用@RequestMapping
而不会出现问题。