如何在ApplicationContext中使WebApplicationContext访问beans



这就是我在Java Web应用程序中引导Spring的方法

public class SpringBootstrap implements WebApplicationInitializer {
    public void onStartup(ServletContext servletContext) throws ServletException {
        final AnnotationConfigApplicationContext rootContext = new AnnotationConfigApplicationContext();
        rootContext.register(RootContextConfiguration.class);
        final AnnotationConfigWebApplicationContext webContext = new AnnotationConfigWebApplicationContext();
        webContext.register(WebContextConfiguration.class);
        webContext.setParent(rootContext);
        final DispatcherServlet dispatcherServlet = new DispatcherServlet(webContext);
        final ServletRegistration.Dynamic dispatcher = servletContext.addServlet("springDispatcher", dispatcherServlet);
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");
        final ContextLoaderListener contextLoadListener = new ContextLoaderListener(webContext);
        servletContext.addListener(contextLoadListener);
    }
}

RootContextConfiguration和WebContextConfiguration类如下:

@Configuration
@ComponentScan(basePackages = "biz.tugay.janeleven.root")
public class RootContextConfiguration {
}
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "biz.tugay.janeleven.web")
public class WebContextConfiguration {
    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        return resolver;
    }
}

当我尝试将应用程序部署到Tomcat并启动时,我会得到:

原因:org.springframework.beans.factory.NoSuchBeanDefinitionException:否类型的限定bean为找到了[biz.tugay.janeleven.root.service.AppUserService]依赖项:需要至少1个符合autowire条件的bean此依赖项的候选项。依赖项注释:{@org.springframework.beans.factory.annotation.Autowired(required=true)}

我还在这里包括HelloServlet和AppUserService类,以及它们的包信息:

包biz.tugay.janeleven.root.service;

@Service
public class AppUserService {
    public AppUser addAppUser(String name) {
        final AppUser appUser = new AppUser(name);
        AppUserDB.addUser(appUser);
        return appUser;
    }
    public Collection<AppUser> getAllAppUsers() {
        return AppUserDB.getAll();
    }
}
package biz.tugay.janeleven.web;
@Controller
@RequestMapping(value = "/")
public class HelloWorldServlet {
    @Autowired
    private AppUserService appUserService;
    @RequestMapping(value = "", method = RequestMethod.GET)
    public String helloWorld(Model model) {
        final Collection<AppUser> appUserList = appUserService.getAllAppUsers();
        model.addAttribute("appUserList", appUserList);
        return "hello.jsp";
    }
    @RequestMapping(value = "", method = RequestMethod.POST)
    public String addApplicationUser(@RequestParam("username") String username) {
        appUserService.addAppUser(username);
        return "redirect:/";
    }
}

我的期望是,WebContextConfiguration应该能够找到由RootContextConfiguration管理的bean,因为我正在调用:

webContext.setParent(rootContext);

在引导过程中。但很明显,我错过了一些东西。

您认为web上下文将从根上下文继承是正确的,但是初始化servletContext.addListener()时没有包含rootContext;请参阅下面的类似代码,这些代码将帮助您解决问题。

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
    rootContext.register(AppConfig.class);
    servletContext.addListener(new ContextLoaderListener(rootContext));
    AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
    dispatcherContext.register(WebConfig.class);
    ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher",
            new DispatcherServlet(dispatcherContext));
    dispatcher.setLoadOnStartup(1);
    dispatcher.addMapping("/");
}

相关内容

  • 没有找到相关文章

最新更新