Java Code Examples for org.springframework.web.context.WebApplicationContext#containsBean()

The following examples show how to use org.springframework.web.context.WebApplicationContext#containsBean() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RedirectView.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * 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;
}
 
Example 2
Source File: SpringBeanFacesELResolver.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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");
			}
		}
	}
}
 
Example 3
Source File: SafeDelegatingFilterProxy.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
protected void initFilterBean() throws ServletException {
    // If no target bean name specified, use filter name.
    if (getTargetBeanName() == null) {
        setTargetBeanName(getFilterName());
    }

    // make sure context is valid and bean exists before enabling this filter
    synchronized (this.delegateMonitor) {
        WebApplicationContext wac = findWebApplicationContext();
        if (wac != null) {
            if (wac.containsBean(getTargetBeanName())) {
                super.initFilterBean();
                enabled = true;
            } else {
                log.info("Can't find a bean with name: " + getTargetBeanName() + ", safely disable proxying");
            }
        } else {
            log.warn("Can't find web application context");
        }
    }
}
 
Example 4
Source File: RedirectView.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * 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;
}
 
Example 5
Source File: RedirectView.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 6
Source File: OpenEntityManagerInViewFilter.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
	}
}
 
Example 7
Source File: SafeDelegatingFilterProxy.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
protected void initFilterBean() throws ServletException {
    // If no target bean name specified, use filter name.
    if (getTargetBeanName() == null) {
        setTargetBeanName(getFilterName());
    }

    // make sure context is valid and bean exists before enabling this filter
    synchronized (this.delegateMonitor) {
        WebApplicationContext wac = findWebApplicationContext();
        if (wac != null) {
            if (wac.containsBean(getTargetBeanName())) {
                super.initFilterBean();
                enabled = true;
            } else {
                log.info("Can't find a bean with name: " + getTargetBeanName() + ", safely disable proxying");
            }
        } else {
            log.warn("Can't find web application context");
        }
    }
}
 
Example 8
Source File: OpenEntityManagerInViewFilter.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * 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);
	}
}
 
Example 9
Source File: SpringBeanFacesELResolver.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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;
}
 
Example 10
Source File: SpringBeanFacesELResolver.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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;
}
 
Example 11
Source File: MultipartFilter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * 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;
	}
}
 
Example 12
Source File: SpringBeanFacesELResolver.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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;
}
 
Example 13
Source File: MultipartFilter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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, or {@code null} if none found
 */
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;
	}
}
 
Example 14
Source File: WebApplicationContextFacesELResolver.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
@Nullable
public Object getValue(ELContext elContext, @Nullable 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;
}
 
Example 15
Source File: SpringBeanFacesELResolver.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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;
}
 
Example 16
Source File: SpringBeanFacesELResolver.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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;
}
 
Example 17
Source File: WebApplicationContextFacesELResolver.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example 18
Source File: MultipartFilter.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * 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;
	}
}
 
Example 19
Source File: WebApplicationContextFacesELResolver.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@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;
}
 
Example 20
Source File: MultipartFilter.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * 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, or {@code null} if none found
 */
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;
	}
}