@Override @Nullable public Object getAttribute(String name) { if ((this.explicitAttributes == null || !this.explicitAttributes.contains(name)) && (this.exposedContextBeanNames == null || this.exposedContextBeanNames.contains(name)) && this.webApplicationContext.containsBean(name)) { return this.webApplicationContext.getBean(name); } else { return super.getAttribute(name); } }
@Override public boolean isReadOnly(ELContext elContext, @Nullable Object base, Object property) throws ELException { if (base == null) { String beanName = property.toString(); WebApplicationContext wac = getWebApplicationContext(elContext); if (wac.containsBean(beanName)) { return true; } } return false; }
@Override @Nullable public Object getValue(ELContext elContext, @Nullable Object base, Object property) throws ELException { if (base == null) { String beanName = property.toString(); WebApplicationContext wac = getWebApplicationContext(elContext); if (wac.containsBean(beanName)) { elContext.setPropertyResolved(true); return wac.getBean(beanName); } } return null; }
@Override @Nullable public Class<?> getType(ELContext elContext, @Nullable Object base, Object property) throws ELException { if (base == null) { String beanName = property.toString(); WebApplicationContext wac = getWebApplicationContext(elContext); if (wac.containsBean(beanName)) { elContext.setPropertyResolved(true); return wac.getType(beanName); } } return null; }
@Override public void setValue(ELContext elContext, @Nullable Object base, Object property, Object value) throws ELException { if (base == null) { String beanName = property.toString(); WebApplicationContext wac = getWebApplicationContext(elContext); if (wac.containsBean(beanName)) { if (value == wac.getBean(beanName)) { // Setting the bean reference to the same value is alright - can simply be ignored... elContext.setPropertyResolved(true); } else { throw new PropertyNotWritableException( "Variable '" + beanName + "' refers to a Spring bean which by definition is not writable"); } } } }
/** * Look for a MultipartResolver bean in the root web application context. * Supports a "multipartResolverBeanName" filter init param; the default * bean name is "filterMultipartResolver". * <p>This can be overridden to use a custom MultipartResolver instance, * for example if not using a Spring web application context. * @return the MultipartResolver instance */ protected MultipartResolver lookupMultipartResolver() { WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(getServletContext()); String beanName = getMultipartResolverBeanName(); if (wac != null && wac.containsBean(beanName)) { if (logger.isDebugEnabled()) { logger.debug("Using MultipartResolver '" + beanName + "' for MultipartFilter"); } return wac.getBean(beanName, MultipartResolver.class); } else { return this.defaultMultipartResolver; } }
public RequestPostProcessor beforeMockMvcCreated(ConfigurableMockMvcBuilder<?> builder, WebApplicationContext context) { if (!context.containsBean(SPRING_SECURITY_FILTER_CHAIN)) { return null; } return configurer.beforeMockMvcCreated(builder, context); } }
@Override public boolean isReadOnly(ELContext elContext, @Nullable Object base, Object property) throws ELException { if (base == null) { String beanName = property.toString(); WebApplicationContext wac = getWebApplicationContext(elContext); if (wac.containsBean(beanName)) { return true; } } return false; }
@Override @Nullable public Object getAttribute(String name) { if ((this.explicitAttributes == null || !this.explicitAttributes.contains(name)) && (this.exposedContextBeanNames == null || this.exposedContextBeanNames.contains(name)) && this.webApplicationContext.containsBean(name)) { return this.webApplicationContext.getBean(name); } else { return super.getAttribute(name); } }
@Override @Nullable public Object getValue(ELContext elContext, @Nullable Object base, Object property) throws ELException { if (base == null) { String beanName = property.toString(); WebApplicationContext wac = getWebApplicationContext(elContext); if (wac.containsBean(beanName)) { elContext.setPropertyResolved(true); return wac.getBean(beanName); } } return null; }
logger.trace("Attempting to resolve property '" + beanName + "' in root WebApplicationContext"); if (wac.containsBean(beanName)) { if (logger.isDebugEnabled()) { logger.debug("Successfully resolved property '" + beanName + "' in root WebApplicationContext");
if (wac.containsBean(beanName)) { T endpoint = wac.getBean(beanName, endpointClass); if (logger.isTraceEnabled()) { if (ann != null && wac.containsBean(ann.value())) { T endpoint = wac.getBean(ann.value(), endpointClass); if (logger.isTraceEnabled()) {
/** * Look up the EntityManagerFactory that this filter should use. * <p>The default implementation looks for a bean with the specified name * in Spring's root application context. * @return the EntityManagerFactory to use * @see #getEntityManagerFactoryBeanName */ protected EntityManagerFactory lookupEntityManagerFactory() { WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext()); String emfBeanName = getEntityManagerFactoryBeanName(); String puName = getPersistenceUnitName(); if (StringUtils.hasLength(emfBeanName)) { return wac.getBean(emfBeanName, EntityManagerFactory.class); } else if (!StringUtils.hasLength(puName) && wac.containsBean(DEFAULT_ENTITY_MANAGER_FACTORY_BEAN_NAME)) { return wac.getBean(DEFAULT_ENTITY_MANAGER_FACTORY_BEAN_NAME, EntityManagerFactory.class); } else { // Includes fallback search for single EntityManagerFactory bean by type. return EntityManagerFactoryUtils.findEntityManagerFactory(wac, puName); } }
/** * Find the registered {@link RequestDataValueProcessor}, if any, and allow * it to update the redirect target URL. * @param targetUrl the given redirect URL * @return the updated URL or the same as URL as the one passed in */ protected String updateTargetUrl(String targetUrl, Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) { WebApplicationContext wac = getWebApplicationContext(); if (wac == null) { wac = RequestContextUtils.findWebApplicationContext(request, getServletContext()); } if (wac != null && wac.containsBean(RequestContextUtils.REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME)) { RequestDataValueProcessor processor = wac.getBean( RequestContextUtils.REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME, RequestDataValueProcessor.class); return processor.processUrl(request, targetUrl); } return targetUrl; }
@Test public void register() throws ServletException { initializer.onStartup(servletContext); assertEquals(1, servlets.size()); assertNotNull(servlets.get(SERVLET_NAME)); DispatcherServlet servlet = (DispatcherServlet) servlets.get(SERVLET_NAME); assertEquals(MyDispatcherServlet.class, servlet.getClass()); WebApplicationContext servletContext = servlet.getWebApplicationContext(); assertTrue(servletContext.containsBean("bean")); assertTrue(servletContext.getBean("bean") instanceof MyBean); assertEquals(1, registrations.size()); assertNotNull(registrations.get(SERVLET_NAME)); MockServletRegistration registration = registrations.get(SERVLET_NAME); assertEquals(Collections.singleton(SERVLET_MAPPING), registration.getMappings()); assertEquals(1, registration.getLoadOnStartup()); assertEquals(ROLE_NAME, registration.getRunAsRole()); }
@Test public void testFrameworkServletWithCustomLocation() throws Exception { DispatcherServlet servlet = new DispatcherServlet(); servlet.setContextConfigLocation("/org/springframework/web/context/WEB-INF/testNamespace.xml " + "/org/springframework/web/context/WEB-INF/context-addition.xml"); servlet.init(new MockServletConfig(new MockServletContext(""), "test")); assertTrue(servlet.getWebApplicationContext().containsBean("kerry")); assertTrue(servlet.getWebApplicationContext().containsBean("kerryX")); }
@Test public void testContextLoaderListenerWithDefaultContext() { MockServletContext sc = new MockServletContext(""); sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "/org/springframework/web/context/WEB-INF/applicationContext.xml " + "/org/springframework/web/context/WEB-INF/context-addition.xml"); ServletContextListener listener = new ContextLoaderListener(); ServletContextEvent event = new ServletContextEvent(sc); listener.contextInitialized(event); String contextAttr = WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE; WebApplicationContext context = (WebApplicationContext) sc.getAttribute(contextAttr); assertTrue("Correct WebApplicationContext exposed in ServletContext", context instanceof XmlWebApplicationContext); assertTrue(WebApplicationContextUtils.getRequiredWebApplicationContext(sc) instanceof XmlWebApplicationContext); LifecycleBean lb = (LifecycleBean) context.getBean("lifecycle"); assertTrue("Has father", context.containsBean("father")); assertTrue("Has rod", context.containsBean("rod")); assertTrue("Has kerry", context.containsBean("kerry")); assertTrue("Not destroyed", !lb.isDestroyed()); assertFalse(context.containsBean("beans1.bean1")); assertFalse(context.containsBean("beans1.bean2")); listener.contextDestroyed(event); assertTrue("Destroyed", lb.isDestroyed()); assertNull(sc.getAttribute(contextAttr)); assertNull(WebApplicationContextUtils.getWebApplicationContext(sc)); }
@Test public void register() throws ServletException { initializer.onStartup(servletContext); assertTrue(eventListener instanceof ContextLoaderListener); ContextLoaderListener cll = (ContextLoaderListener) eventListener; cll.contextInitialized(new ServletContextEvent(servletContext)); WebApplicationContext applicationContext = WebApplicationContextUtils .getRequiredWebApplicationContext(servletContext); assertTrue(applicationContext.containsBean(BEAN_NAME)); assertTrue(applicationContext.getBean(BEAN_NAME) instanceof MyBean); }
@Test public void register() throws ServletException { initializer.onStartup(servletContext); assertEquals(1, servlets.size()); assertNotNull(servlets.get(SERVLET_NAME)); DispatcherServlet servlet = (DispatcherServlet) servlets.get(SERVLET_NAME); WebApplicationContext wac = servlet.getWebApplicationContext(); ((AnnotationConfigWebApplicationContext) wac).refresh(); assertTrue(wac.containsBean("bean")); assertTrue(wac.getBean("bean") instanceof MyBean); assertEquals(1, servletRegistrations.size()); assertNotNull(servletRegistrations.get(SERVLET_NAME)); MockServletRegistration servletRegistration = servletRegistrations.get(SERVLET_NAME); assertEquals(Collections.singleton(SERVLET_MAPPING), servletRegistration.getMappings()); assertEquals(1, servletRegistration.getLoadOnStartup()); assertEquals(ROLE_NAME, servletRegistration.getRunAsRole()); assertTrue(servletRegistration.isAsyncSupported()); assertEquals(4, filterRegistrations.size()); assertNotNull(filterRegistrations.get("hiddenHttpMethodFilter")); assertNotNull(filterRegistrations.get("delegatingFilterProxy")); assertNotNull(filterRegistrations.get("delegatingFilterProxy#0")); assertNotNull(filterRegistrations.get("delegatingFilterProxy#1")); for (MockFilterRegistration filterRegistration : filterRegistrations.values()) { assertTrue(filterRegistration.isAsyncSupported()); EnumSet<DispatcherType> enumSet = EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.INCLUDE, DispatcherType.ASYNC); assertEquals(enumSet, filterRegistration.getMappings().get(SERVLET_NAME)); } }
@Test public void rootContextOnly() throws ServletException { initializer = new MyAnnotationConfigDispatcherServletInitializer() { @Override protected Class<?>[] getRootConfigClasses() { return new Class<?>[] {MyConfiguration.class}; } @Override protected Class<?>[] getServletConfigClasses() { return null; } }; initializer.onStartup(servletContext); DispatcherServlet servlet = (DispatcherServlet) servlets.get(SERVLET_NAME); servlet.init(new MockServletConfig(this.servletContext)); WebApplicationContext wac = servlet.getWebApplicationContext(); ((AnnotationConfigWebApplicationContext) wac).refresh(); assertTrue(wac.containsBean("bean")); assertTrue(wac.getBean("bean") instanceof MyBean); }