/** * Initialize the view bean factory from the XML file. * Synchronized because of access by parallel threads. * @throws BeansException in case of initialization errors */ protected synchronized BeanFactory initFactory() throws BeansException { if (this.cachedFactory != null) { return this.cachedFactory; } ApplicationContext applicationContext = obtainApplicationContext(); Resource actualLocation = this.location; if (actualLocation == null) { actualLocation = applicationContext.getResource(DEFAULT_LOCATION); } // Create child ApplicationContext for views. GenericWebApplicationContext factory = new GenericWebApplicationContext(); factory.setParent(applicationContext); factory.setServletContext(getServletContext()); // Load XML resource with context-aware entity resolver. XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory); reader.setEnvironment(applicationContext.getEnvironment()); reader.setEntityResolver(new ResourceEntityResolver(applicationContext)); reader.loadBeanDefinitions(actualLocation); factory.refresh(); if (isCache()) { this.cachedFactory = factory; } return factory; }
@Override protected WebApplicationContext createWebApplicationContext(@Nullable WebApplicationContext parent) { GenericWebApplicationContext wac = new GenericWebApplicationContext(); wac.registerBeanDefinition("controller", new RootBeanDefinition(controllerclass)); DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator(); autoProxyCreator.setBeanFactory(wac.getBeanFactory()); wac.getBeanFactory().addBeanPostProcessor(autoProxyCreator); wac.getBeanFactory().registerSingleton("advisor", new DefaultPointcutAdvisor(new SimpleTraceInterceptor(true))); wac.refresh(); return wac; } };
@Test public void registerServletParamPropertySources_GenericWebApplicationContext() { MockServletContext servletContext = new MockServletContext(); servletContext.addInitParameter("pCommon", "pCommonContextValue"); servletContext.addInitParameter("pContext1", "pContext1Value"); GenericWebApplicationContext ctx = new GenericWebApplicationContext(); ctx.setServletContext(servletContext); ctx.refresh(); ConfigurableEnvironment environment = ctx.getEnvironment(); assertThat(environment, instanceOf(StandardServletEnvironment.class)); MutablePropertySources propertySources = environment.getPropertySources(); assertThat(propertySources.contains(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME), is(true)); // ServletContext params are available assertThat(environment.getProperty("pCommon"), is("pCommonContextValue")); assertThat(environment.getProperty("pContext1"), is("pContext1Value")); // Servlet* PropertySources have precedence over System* PropertySources assertThat(propertySources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)), lessThan(propertySources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME)))); // Replace system properties with a mock property source for convenience MockPropertySource mockSystemProperties = new MockPropertySource(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME); mockSystemProperties.setProperty("pCommon", "pCommonSysPropsValue"); mockSystemProperties.setProperty("pSysProps1", "pSysProps1Value"); propertySources.replace(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, mockSystemProperties); // assert that servletcontext init params resolve with higher precedence than sysprops assertThat(environment.getProperty("pCommon"), is("pCommonContextValue")); assertThat(environment.getProperty("pSysProps1"), is("pSysProps1Value")); }
public HandlerMethodAnnotationDetectionTests(Class<?> controllerType, boolean useAutoProxy) { GenericWebApplicationContext context = new GenericWebApplicationContext(); context.registerBeanDefinition("controller", new RootBeanDefinition(controllerType)); context.registerBeanDefinition("handlerMapping", new RootBeanDefinition(RequestMappingHandlerMapping.class)); context.registerBeanDefinition("handlerAdapter", new RootBeanDefinition(RequestMappingHandlerAdapter.class)); context.registerBeanDefinition("exceptionResolver", new RootBeanDefinition(ExceptionHandlerExceptionResolver.class)); if (useAutoProxy) { DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator(); autoProxyCreator.setBeanFactory(context.getBeanFactory()); context.getBeanFactory().addBeanPostProcessor(autoProxyCreator); context.registerBeanDefinition("controllerAdvice", new RootBeanDefinition(ControllerAdvisor.class)); } context.refresh(); this.handlerMapping = context.getBean(RequestMappingHandlerMapping.class); this.handlerAdapter = context.getBean(RequestMappingHandlerAdapter.class); this.exceptionResolver = context.getBean(ExceptionHandlerExceptionResolver.class); context.close(); }
@Before public void init() { GenericWebApplicationContext applicationContext = new GenericWebApplicationContext(); applicationContext.refresh(); servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, applicationContext); }
GenericWebApplicationContext context = new GenericWebApplicationContext(); context.setParent(parent); customizeBeanFactory(context.getDefaultListableBeanFactory(), webMergedConfig); loadBeanDefinitions(context, webMergedConfig); AnnotationConfigUtils.registerAnnotationConfigProcessors(context); customizeContext(context, webMergedConfig); context.refresh(); context.registerShutdownHook(); return context;
public class XmlWebApplicationContextLoader extends AbstractContextLoader { public final ConfigurableApplicationContext loadContext(final String... locations) throws Exception { ServletContext servletContext = new MockServletContext("war", new FileSystemResourceLoader()); GenericWebApplicationContext webContext = new GenericWebApplicationContext(); servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, webContext); webContext.setServletContext(servletContext); new XmlBeanDefinitionReader(webContext).loadBeanDefinitions(locations); AnnotationConfigUtils.registerAnnotationConfigProcessors(webContext); webContext.refresh(); webContext.registerShutdownHook(); return webContext; } protected String getResourceSuffix() { return ""; }
private GenericApplicationContext createFlowApplicationContext(Resource[] resources) { ApplicationContext parent = getContext().getApplicationContext(); GenericApplicationContext flowContext; if (parent instanceof WebApplicationContext) { GenericWebApplicationContext webContext = new GenericWebApplicationContext(); webContext.setServletContext(((WebApplicationContext) parent).getServletContext()); flowContext = webContext; } else { flowContext = new GenericApplicationContext(); } flowContext.setDisplayName("Flow ApplicationContext [" + getContext().getFlowId() + "]"); flowContext.setParent(parent); flowContext.getBeanFactory().registerScope("request", new RequestScope()); flowContext.getBeanFactory().registerScope("flash", new FlashScope()); flowContext.getBeanFactory().registerScope("view", new ViewScope()); flowContext.getBeanFactory().registerScope("flow", new FlowScope()); flowContext.getBeanFactory().registerScope("conversation", new ConversationScope()); // Ensure the current ClassLoader is used, or otherwise setting the ResourceLoader would suppress it ClassLoader classLoaderToUse = flowContext.getClassLoader(); flowContext.setClassLoader(classLoaderToUse); Resource flowResource = flowModelHolder.getFlowModelResource(); flowContext.setResourceLoader(new FlowRelativeResourceLoader(flowResource)); AnnotationConfigUtils.registerAnnotationConfigProcessors(flowContext); new XmlBeanDefinitionReader(flowContext).loadBeanDefinitions(resources); registerFlowBeans(flowContext.getBeanFactory()); registerMessageSource(flowContext, flowResource); flowContext.refresh(); return flowContext; }
private void loadBeanDefinitions(String fileName) { XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this.appContext); ClassPathResource resource = new ClassPathResource(fileName, MessageBrokerBeanDefinitionParserTests.class); reader.loadBeanDefinitions(resource); this.appContext.setServletContext(new MockServletContext()); this.appContext.refresh(); }
@Before @SuppressWarnings("resource") public void setUp() throws Exception { GenericWebApplicationContext context = new GenericWebApplicationContext(); context.refresh(); resolver = new ExpressionValueMethodArgumentResolver(context.getBeanFactory()); Method method = getClass().getMethod("params", int.class, String.class, String.class); paramSystemProperty = new MethodParameter(method, 0); paramContextPath = new MethodParameter(method, 1); paramNotSupported = new MethodParameter(method, 2); webRequest = new ServletWebRequest(new MockHttpServletRequest(), new MockHttpServletResponse()); // Expose request to the current thread (for SpEL expressions) RequestContextHolder.setRequestAttributes(webRequest); }
private WebApplicationContext initApplicationContext(String scope) { MockServletContext sc = new MockServletContext(); GenericWebApplicationContext ac = new GenericWebApplicationContext(sc); GenericBeanDefinition bd = new GenericBeanDefinition(); bd.setBeanClass(DerivedTestBean.class); bd.setScope(scope); ac.registerBeanDefinition(NAME, bd); ac.refresh(); return ac; }
protected void setupSpringContext() { GenericWebApplicationContext context = new GenericWebApplicationContext(); new AnnotatedBeanDefinitionReader(context).register(GeoGigWebAPISpringConfig.class); MockServletContext msc = new MockServletContext(""); context.setServletContext(msc); context.refresh(); wac = context; } /**
@Test public void noResourceHandlers() throws Exception { this.registry = new ResourceHandlerRegistry(new GenericWebApplicationContext(), new MockServletContext()); assertNull(this.registry.getHandlerMapping()); }
@Test public void webApplicationContext() { GenericWebApplicationContext ctx = new GenericWebApplicationContext(newBeanFactoryWithEnvironmentAwareBean()); assertHasStandardServletEnvironment(ctx); ctx.setEnvironment(prodWebEnv); ctx.refresh(); assertHasEnvironment(ctx, prodWebEnv); assertEnvironmentBeanRegistered(ctx); assertEnvironmentAwareInvoked(ctx, prodWebEnv); }
private GenericWebApplicationContext autowireWebServerGenericWebApplicationContext() { GenericWebApplicationContext context = new GenericWebApplicationContext(); context.registerBean("webHandler", DispatcherHandler.class); this.spring.context(context).autowire(); return (GenericWebApplicationContext) this.spring.getContext(); } }
public void setup(ApplicationContext applicationContext) throws ServletException { MockServletContext servletContext = new MockServletContext(); GenericWebApplicationContext testWebContext = new GenericWebApplicationContext(); testWebContext.setParent(applicationContext); testWebContext.setServletContext(servletContext); servletContext.setAttribute(CONTEXT_NAME, testWebContext); servletContext.setContextPath(CONTEXT_PATH); config = new MockServletConfig(servletContext, "services"); Assert.assertNotNull(config); d.setPublishEvents(false); d.setContextAttribute(CONTEXT_NAME); d.init(config); }
private ApplicationContext createContext(final ScopedProxyMode scopedProxyMode) { GenericWebApplicationContext context = new GenericWebApplicationContext(); ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context); scanner.setIncludeAnnotationConfig(false); scanner.scan(getClass().getPackage().getName()); context.registerAlias("classPathBeanDefinitionScannerJsr330ScopeIntegrationTests.SessionScopedTestBean", "session"); context.refresh(); return context;
@Override public void setServletContext(ServletContext servletContext) { super.setServletContext(servletContext); // prepareWebApplicationContext(servletContext); }
WebMergedContextConfiguration webMergedConfig) { ApplicationContext parent = context.getParent(); ServletContext servletContext = new MockServletContext(resourceBasePath, resourceLoader); servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, context); context.setServletContext(servletContext); context.setServletContext(servletContext);
String strContextFile = "file:" + strConfPath + FILE_CORE_CONTEXT; GenericWebApplicationContext gwac = new GenericWebApplicationContext( servletContext ); gwac.setId( getContextName( servletContext ) ); gwac.refresh( );