@Test public void annotationConfigWebApplicationContext() { AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); ctx.setEnvironment(prodWebEnv); ctx.setConfigLocation(EnvironmentAwareBean.class.getName()); ctx.refresh(); assertHasEnvironment(ctx, prodWebEnv); assertEnvironmentBeanRegistered(ctx); assertEnvironmentAwareInvoked(ctx, prodWebEnv); }
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); context.setEnvironment(environment);
protected WebApplicationContext applicationContext() { if (applicationContext == null) { AnnotationConfigWebApplicationContext webApplicationContext = new AnnotationConfigWebApplicationContext(); Set<Class<?>> annotatedClasses = annotatedClasses(); if (annotatedClasses != null) { annotatedClasses.iterator().forEachRemaining(webApplicationContext::register); } Set<? extends BeanFactoryPostProcessor> beanFactoryPostProcessors = beanFactoryPostProcessors(); if (beanFactoryPostProcessors != null) { beanFactoryPostProcessors.iterator().forEachRemaining(webApplicationContext::addBeanFactoryPostProcessor); } if (this.rootApplicationContext != null) { webApplicationContext.setParent(this.rootApplicationContext); webApplicationContext.setEnvironment((ConfigurableEnvironment) rootApplicationContext.getEnvironment()); } applicationContext = webApplicationContext; } return (WebApplicationContext) applicationContext; }
@Bean public ServletRegistrationBean<DispatcherServlet> dispatcherServletRegistrationBean(ConfigurableEnvironment environment) { ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(); // 构建 DispatcherServlet 应用上下文 AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); // 复用当前 ApplicationContext 的 ConfigurableEnvironment 对象 context.setEnvironment(environment); // 设置 DispatcherServlet servletRegistrationBean.setServlet(new DispatcherServlet(context)); // 设置 ServletConfig 初始化参数 servletRegistrationBean.addInitParameter("my-servlet-name", "My DispatcherServlet"); return servletRegistrationBean; }
protected WebApplicationContext applicationContext() { if (applicationContext == null) { AnnotationConfigWebApplicationContext webApplicationContext = new AnnotationConfigWebApplicationContext(); Set<Class<?>> annotatedClasses = annotatedClasses(); if (annotatedClasses != null) { annotatedClasses.iterator().forEachRemaining(webApplicationContext::register); } Set<? extends BeanFactoryPostProcessor> beanFactoryPostProcessors = beanFactoryPostProcessors(); if (beanFactoryPostProcessors != null) { beanFactoryPostProcessors.iterator().forEachRemaining(webApplicationContext::addBeanFactoryPostProcessor); } if (this.rootApplicationContext != null) { webApplicationContext.setParent(this.rootApplicationContext); webApplicationContext.setEnvironment((ConfigurableEnvironment) rootApplicationContext.getEnvironment()); } applicationContext = webApplicationContext; } return (WebApplicationContext) applicationContext; }
protected WebApplicationContext applicationContext() { if (applicationContext == null) { AnnotationConfigWebApplicationContext webApplicationContext = new AnnotationConfigWebApplicationContext(); Set<Class<?>> annotatedClasses = annotatedClasses(); if (annotatedClasses != null) { annotatedClasses.iterator().forEachRemaining(webApplicationContext::register); } Set<? extends BeanFactoryPostProcessor> beanFactoryPostProcessors = beanFactoryPostProcessors(); if (beanFactoryPostProcessors != null) { beanFactoryPostProcessors.iterator().forEachRemaining(webApplicationContext::addBeanFactoryPostProcessor); } if (this.rootApplicationContext != null) { webApplicationContext.setParent(this.rootApplicationContext); webApplicationContext.setEnvironment((ConfigurableEnvironment) rootApplicationContext.getEnvironment()); } applicationContext = webApplicationContext; } return (WebApplicationContext) applicationContext; }
@Override protected void doStart() throws Exception { AbstractHandler noContentHandler = new NoContentOutputErrorHandler(); // This part is needed to avoid WARN while starting container. noContentHandler.setServer(server); server.addBean(noContentHandler); // Create the servlet context final ServletContextHandler context = new ServletContextHandler(server, entrypoint, ServletContextHandler.SESSIONS); // REST configuration final ServletHolder servletHolder = new ServletHolder(ServletContainer.class); servletHolder.setInitParameter("javax.ws.rs.Application", GraviteeApplication.class.getName()); servletHolder.setInitOrder(0); context.addServlet(servletHolder, "/*"); // Spring configuration System.setProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, "basic"); AnnotationConfigWebApplicationContext webApplicationContext = new AnnotationConfigWebApplicationContext(); webApplicationContext.register(SecurityConfiguration.class); webApplicationContext.setEnvironment((ConfigurableEnvironment) applicationContext.getEnvironment()); webApplicationContext.setParent(applicationContext); context.addEventListener(new ContextLoaderListener(webApplicationContext)); // Spring Security filter context.addFilter(new FilterHolder(new DelegatingFilterProxy("springSecurityFilterChain")),"/*", EnumSet.allOf(DispatcherType.class)); // start the server server.start(); }
@Bean(name=HTTP_TRANSPORT_CHILD_CONTEXT_BEAN_NAME) public SpringContextWrapper transportWebMvcContext(ConfigurableWebApplicationContext parentContext, ServletContextHandler servletContextHandler){ AnnotationConfigWebApplicationContext transportWebMvcContext = new AnnotationConfigWebApplicationContext(); transportWebMvcContext.setDisplayName("httpTransport-webMvcContext"); transportWebMvcContext.setServletContext(servletContextHandler.getServletContext()); transportWebMvcContext.setParent(parentContext); transportWebMvcContext.setEnvironment(parentContext.getEnvironment()); transportWebMvcContext.register(SpringMvcConfiguration.class); transportWebMvcContext.register(PropertySourcesPlaceholderConfigurer.class); transportWebMvcContext.refresh(); return new SpringContextWrapper(transportWebMvcContext); }