use of org.springframework.web.context.WebApplicationContext in project spring-framework by spring-projects.
the class WebApplicationContextFacesELResolver method getType.
@Override
public Class<?> getType(ELContext elContext, Object base, Object property) throws ELException {
if (base != null) {
if (base instanceof WebApplicationContext) {
WebApplicationContext wac = (WebApplicationContext) base;
String beanName = property.toString();
if (logger.isDebugEnabled()) {
logger.debug("Attempting to resolve property '" + beanName + "' in root WebApplicationContext");
}
if (wac.containsBean(beanName)) {
if (logger.isDebugEnabled()) {
logger.debug("Successfully resolved property '" + beanName + "' in root WebApplicationContext");
}
elContext.setPropertyResolved(true);
try {
return wac.getType(beanName);
} catch (BeansException ex) {
throw new ELException(ex);
}
} else {
// Mimic standard JSF/JSP behavior when base is a Map by returning null.
return null;
}
}
} else {
if (WEB_APPLICATION_CONTEXT_VARIABLE_NAME.equals(property)) {
elContext.setPropertyResolved(true);
return WebApplicationContext.class;
}
}
return null;
}
use of org.springframework.web.context.WebApplicationContext in project spring-framework by spring-projects.
the class WebApplicationContextFacesELResolver method getValue.
@Override
public Object getValue(ELContext elContext, Object base, Object property) throws ELException {
if (base != null) {
if (base instanceof WebApplicationContext) {
WebApplicationContext wac = (WebApplicationContext) base;
String beanName = property.toString();
if (logger.isTraceEnabled()) {
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");
}
elContext.setPropertyResolved(true);
try {
return wac.getBean(beanName);
} catch (BeansException ex) {
throw new ELException(ex);
}
} else {
// Mimic standard JSF/JSP behavior when base is a Map by returning null.
return null;
}
}
} else {
if (WEB_APPLICATION_CONTEXT_VARIABLE_NAME.equals(property)) {
elContext.setPropertyResolved(true);
return getWebApplicationContext(elContext);
}
}
return null;
}
use of org.springframework.web.context.WebApplicationContext in project spring-framework by spring-projects.
the class WebApplicationContextScopeTests method testRequestScope.
@Test
public void testRequestScope() {
WebApplicationContext ac = initApplicationContext(WebApplicationContext.SCOPE_REQUEST);
MockHttpServletRequest request = new MockHttpServletRequest();
ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request);
RequestContextHolder.setRequestAttributes(requestAttributes);
try {
assertNull(request.getAttribute(NAME));
DerivedTestBean bean = ac.getBean(NAME, DerivedTestBean.class);
assertSame(bean, request.getAttribute(NAME));
assertSame(bean, ac.getBean(NAME));
requestAttributes.requestCompleted();
assertTrue(bean.wasDestroyed());
} finally {
RequestContextHolder.setRequestAttributes(null);
}
}
use of org.springframework.web.context.WebApplicationContext in project spring-framework by spring-projects.
the class FrameworkServlet method initWebApplicationContext.
/**
* Initialize and publish the WebApplicationContext for this servlet.
* <p>Delegates to {@link #createWebApplicationContext} for actual creation
* of the context. Can be overridden in subclasses.
* @return the WebApplicationContext instance
* @see #FrameworkServlet(WebApplicationContext)
* @see #setContextClass
* @see #setContextConfigLocation
*/
protected WebApplicationContext initWebApplicationContext() {
WebApplicationContext rootContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
WebApplicationContext wac = null;
if (this.webApplicationContext != null) {
// A context instance was injected at construction time -> use it
wac = this.webApplicationContext;
if (wac instanceof ConfigurableWebApplicationContext) {
ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) wac;
if (!cwac.isActive()) {
// setting the parent context, setting the application context id, etc
if (cwac.getParent() == null) {
// The context instance was injected without an explicit parent -> set
// the root application context (if any; may be null) as the parent
cwac.setParent(rootContext);
}
configureAndRefreshWebApplicationContext(cwac);
}
}
}
if (wac == null) {
// No context instance was injected at construction time -> see if one
// has been registered in the servlet context. If one exists, it is assumed
// that the parent context (if any) has already been set and that the
// user has performed any initialization such as setting the context id
wac = findWebApplicationContext();
}
if (wac == null) {
// No context instance is defined for this servlet -> create a local one
wac = createWebApplicationContext(rootContext);
}
if (!this.refreshEventReceived) {
// Either the context is not a ConfigurableApplicationContext with refresh
// support or the context injected at construction time had already been
// refreshed -> trigger initial onRefresh manually here.
onRefresh(wac);
}
if (this.publishContext) {
// Publish the context as a servlet context attribute.
String attrName = getServletContextAttributeName();
getServletContext().setAttribute(attrName, wac);
if (this.logger.isDebugEnabled()) {
this.logger.debug("Published WebApplicationContext of servlet '" + getServletName() + "' as ServletContext attribute with name [" + attrName + "]");
}
}
return wac;
}
use of org.springframework.web.context.WebApplicationContext in project spring-framework by spring-projects.
the class FrameworkServlet method refresh.
/**
* Refresh this servlet's application context, as well as the
* dependent state of the servlet.
* @see #getWebApplicationContext()
* @see org.springframework.context.ConfigurableApplicationContext#refresh()
*/
public void refresh() {
WebApplicationContext wac = getWebApplicationContext();
if (!(wac instanceof ConfigurableApplicationContext)) {
throw new IllegalStateException("WebApplicationContext does not support refresh: " + wac);
}
((ConfigurableApplicationContext) wac).refresh();
}
Aggregations