如何在ApplicationContext中创建WebApplicationContext访问bean?

时间:2016-01-12 12:38:30

标签: spring spring-mvc autowired managed-bean applicationcontext

这就是我尝试在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个bean有资格成为autowire   这种依赖的候选人。依赖注释:   {@ org.springframework.beans.factory.annotation.Autowired(所需=真)}

我还在这里包含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);

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

1 个答案:

答案 0 :(得分:1)

但是,正确的是,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("/");

}