Java Code Examples for org.springframework.web.context.ContextLoader#getCurrentWebApplicationContext()

The following examples show how to use org.springframework.web.context.ContextLoader#getCurrentWebApplicationContext() . 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: SpringBeanAutowiringSupport.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Process {@code @Autowired} injection for the given target object,
 * based on the current web application context.
 * <p>Intended for use as a delegate.
 * @param target the target object to process
 * @see org.springframework.web.context.ContextLoader#getCurrentWebApplicationContext()
 */
public static void processInjectionBasedOnCurrentContext(Object target) {
	Assert.notNull(target, "Target object must not be null");
	WebApplicationContext cc = ContextLoader.getCurrentWebApplicationContext();
	if (cc != null) {
		AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
		bpp.setBeanFactory(cc.getAutowireCapableBeanFactory());
		bpp.processInjection(target);
	}
	else {
		if (logger.isDebugEnabled()) {
			logger.debug("Current WebApplicationContext is not available for processing of " +
					ClassUtils.getShortName(target.getClass()) + ": " +
					"Make sure this class gets constructed in a Spring web application. Proceeding without injection.");
		}
	}
}
 
Example 2
Source File: RequestContextUtils.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Look for the WebApplicationContext associated with the DispatcherServlet
 * that has initiated request processing, and for the global context if none
 * was found associated with the current request. The global context will
 * be found via the ServletContext or via ContextLoader's current context.
 * <p>NOTE: This variant remains compatible with Servlet 2.5, explicitly
 * checking a given ServletContext instead of deriving it from the request.
 * @param request current HTTP request
 * @param servletContext current servlet context
 * @return the request-specific WebApplicationContext, or the global one
 * if no request-specific context has been found, or {@code null} if none
 * @since 4.2.1
 * @see DispatcherServlet#WEB_APPLICATION_CONTEXT_ATTRIBUTE
 * @see WebApplicationContextUtils#getWebApplicationContext(ServletContext)
 * @see ContextLoader#getCurrentWebApplicationContext()
 */
@Nullable
public static WebApplicationContext findWebApplicationContext(
		HttpServletRequest request, @Nullable ServletContext servletContext) {

	WebApplicationContext webApplicationContext = (WebApplicationContext) request.getAttribute(
			DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE);
	if (webApplicationContext == null) {
		if (servletContext != null) {
			webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
		}
		if (webApplicationContext == null) {
			webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
		}
	}
	return webApplicationContext;
}
 
Example 3
Source File: SpringBeanAutowiringSupport.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Process {@code @Autowired} injection for the given target object,
 * based on the current web application context.
 * <p>Intended for use as a delegate.
 * @param target the target object to process
 * @see org.springframework.web.context.ContextLoader#getCurrentWebApplicationContext()
 */
public static void processInjectionBasedOnCurrentContext(Object target) {
	Assert.notNull(target, "Target object must not be null");
	WebApplicationContext cc = ContextLoader.getCurrentWebApplicationContext();
	if (cc != null) {
		AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
		bpp.setBeanFactory(cc.getAutowireCapableBeanFactory());
		bpp.processInjection(target);
	}
	else {
		if (logger.isDebugEnabled()) {
			logger.debug("Current WebApplicationContext is not available for processing of " +
					ClassUtils.getShortName(target.getClass()) + ": " +
					"Make sure this class gets constructed in a Spring web application. Proceeding without injection.");
		}
	}
}
 
Example 4
Source File: SpringWebConstraintValidatorFactory.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Retrieve the Spring {@link WebApplicationContext} to use.
 * The default implementation returns the current {@link WebApplicationContext}
 * as registered for the thread context class loader.
 * @return the current WebApplicationContext (never {@code null})
 * @see ContextLoader#getCurrentWebApplicationContext()
 */
protected WebApplicationContext getWebApplicationContext() {
	WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
	if (wac == null) {
		throw new IllegalStateException("No WebApplicationContext registered for current thread - " +
				"consider overriding SpringWebConstraintValidatorFactory.getWebApplicationContext()");
	}
	return wac;
}
 
Example 5
Source File: HelloWorldController.java    From tutorials with MIT License 5 votes vote down vote up
private void processContext() {
    WebApplicationContext rootContext = ContextLoader.getCurrentWebApplicationContext();
    
    System.out.println("root context : " + rootContext);
    System.out.println("root context Beans: " + Arrays.asList(rootContext.getBeanDefinitionNames()));

    System.out.println("context : " + webApplicationContext);
    System.out.println("context Beans: " + Arrays.asList(webApplicationContext.getBeanDefinitionNames()));
}
 
Example 6
Source File: SpringWebConstraintValidatorFactory.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Retrieve the Spring {@link WebApplicationContext} to use.
 * The default implementation returns the current {@link WebApplicationContext}
 * as registered for the thread context class loader.
 * @return the current WebApplicationContext (never {@code null})
 * @see ContextLoader#getCurrentWebApplicationContext()
 */
protected WebApplicationContext getWebApplicationContext() {
	WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
	if (wac == null) {
		throw new IllegalStateException("No WebApplicationContext registered for current thread - " +
				"consider overriding SpringWebConstraintValidatorFactory.getWebApplicationContext()");
	}
	return wac;
}
 
Example 7
Source File: SpringWebConstraintValidatorFactory.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Retrieve the Spring {@link WebApplicationContext} to use.
 * The default implementation returns the current {@link WebApplicationContext}
 * as registered for the thread context class loader.
 * @return the current WebApplicationContext (never {@code null})
 * @see ContextLoader#getCurrentWebApplicationContext()
 */
protected WebApplicationContext getWebApplicationContext() {
	WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
	if (wac == null) {
		throw new IllegalStateException("No WebApplicationContext registered for current thread - " +
				"consider overriding SpringWebConstraintValidatorFactory.getWebApplicationContext()");
	}
	return wac;
}
 
Example 8
Source File: ResponseFactory.java    From dpCms with Apache License 2.0 5 votes vote down vote up
private static void initValidator() {
	if (validator == null) {
		WebApplicationContext wac = ContextLoader
				.getCurrentWebApplicationContext();
		ValidatorFactory validatorFactory = (ValidatorFactory) wac
				.getBean("validator");
		validator = validatorFactory.getValidator();
	}
}
 
Example 9
Source File: ExportResponsesBean.java    From sakai with Educational Community License v2.0 4 votes vote down vote up
/**
 * Creates a new TotalScoresBean object.
 */
public ExportResponsesBean() {
	this(ContextLoader.getCurrentWebApplicationContext());
	log.debug("Creating a new ExportResponsesBean");
}
 
Example 10
Source File: RequestContextUtils.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Look for the WebApplicationContext associated with the DispatcherServlet
 * that has initiated request processing, and for the global context if none
 * was found associated with the current request. The global context will
 * be found via the ServletContext or via ContextLoader's current context.
 * <p>NOTE: This variant remains compatible with Servlet 2.5, explicitly
 * checking a given ServletContext instead of deriving it from the request.
 * @param request current HTTP request
 * @param servletContext current servlet context
 * @return the request-specific WebApplicationContext, or the global one
 * if no request-specific context has been found, or {@code null} if none
 * @since 4.2.1
 * @see DispatcherServlet#WEB_APPLICATION_CONTEXT_ATTRIBUTE
 * @see WebApplicationContextUtils#getWebApplicationContext(ServletContext)
 * @see ContextLoader#getCurrentWebApplicationContext()
 */
public static WebApplicationContext findWebApplicationContext(
		HttpServletRequest request, ServletContext servletContext) {

	WebApplicationContext webApplicationContext = (WebApplicationContext) request.getAttribute(
			DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE);
	if (webApplicationContext == null) {
		if (servletContext != null) {
			webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
		}
		if (webApplicationContext == null) {
			webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
		}
	}
	return webApplicationContext;
}
 
Example 11
Source File: RequestContextUtils.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Look for the WebApplicationContext associated with the DispatcherServlet
 * that has initiated request processing, and for the global context if none
 * was found associated with the current request. The global context will
 * be found via the ServletContext or via ContextLoader's current context.
 * <p>NOTE: This variant remains compatible with Servlet 2.5, explicitly
 * checking a given ServletContext instead of deriving it from the request.
 * @param request current HTTP request
 * @param servletContext current servlet context
 * @return the request-specific WebApplicationContext, or the global one
 * if no request-specific context has been found, or {@code null} if none
 * @since 4.2.1
 * @see DispatcherServlet#WEB_APPLICATION_CONTEXT_ATTRIBUTE
 * @see WebApplicationContextUtils#getWebApplicationContext(ServletContext)
 * @see ContextLoader#getCurrentWebApplicationContext()
 */
public static WebApplicationContext findWebApplicationContext(
		HttpServletRequest request, ServletContext servletContext) {

	WebApplicationContext webApplicationContext = (WebApplicationContext) request.getAttribute(
			DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE);
	if (webApplicationContext == null) {
		if (servletContext != null) {
			webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
		}
		if (webApplicationContext == null) {
			webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
		}
	}
	return webApplicationContext;
}
 
Example 12
Source File: AssessmentSettingsBean.java    From sakai with Educational Community License v2.0 4 votes vote down vote up
public AssessmentSettingsBean() {
  this(ContextLoader.getCurrentWebApplicationContext());
}
 
Example 13
Source File: JavaHome.java    From JavaMonitor with Apache License 2.0 4 votes vote down vote up
public static String getPath(){
    WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
    JavaHome javaHome = (JavaHome) wac.getBean("javaHome");
    return javaHome.getBin();
}
 
Example 14
Source File: SessionUtils.java    From flash-waimai with MIT License 4 votes vote down vote up
public static ServletContext getServletContext() {
	WebApplicationContext context = ContextLoader.getCurrentWebApplicationContext();
	return context.getServletContext();
}
 
Example 15
Source File: AssessmentSettingsBean.java    From sakai with Educational Community License v2.0 4 votes vote down vote up
public AssessmentSettingsBean() {
  this(ContextLoader.getCurrentWebApplicationContext());
}
 
Example 16
Source File: SessionUtils.java    From web-flash with MIT License 4 votes vote down vote up
public static ServletContext getServletContext() {
	WebApplicationContext context = ContextLoader.getCurrentWebApplicationContext();
	return context.getServletContext();
}
 
Example 17
Source File: PublishedAssessmentSettingsBean.java    From sakai with Educational Community License v2.0 4 votes vote down vote up
public PublishedAssessmentSettingsBean() {
  this(ContextLoader.getCurrentWebApplicationContext());
}
 
Example 18
Source File: BaseController.java    From admin-plus with Apache License 2.0 4 votes vote down vote up
/**
 * 获取ServletContext
 * 
 * @return
 */
public ServletContext getServletContent() {
	WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
	ServletContext servletContext = webApplicationContext.getServletContext();
	return servletContext;
}
 
Example 19
Source File: ConvertingEncoderDecoderSupport.java    From spring4-understanding with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the active {@link ApplicationContext}. Be default this method obtains
 * the context via {@link ContextLoader#getCurrentWebApplicationContext()}, which
 * finds the ApplicationContext loaded via {@link ContextLoader} typically in a
 * Servlet container environment. When not running in a Servlet container and
 * not using {@link ContextLoader}, this method should be overridden.
 * @return the {@link ApplicationContext} or {@code null}
 */
protected ApplicationContext getApplicationContext() {
	return ContextLoader.getCurrentWebApplicationContext();
}
 
Example 20
Source File: ConvertingEncoderDecoderSupport.java    From java-technology-stack with MIT License 2 votes vote down vote up
/**
 * Returns the active {@link ApplicationContext}. Be default this method obtains
 * the context via {@link ContextLoader#getCurrentWebApplicationContext()}, which
 * finds the ApplicationContext loaded via {@link ContextLoader} typically in a
 * Servlet container environment. When not running in a Servlet container and
 * not using {@link ContextLoader}, this method should be overridden.
 * @return the {@link ApplicationContext} or {@code null}
 */
@Nullable
protected ApplicationContext getApplicationContext() {
	return ContextLoader.getCurrentWebApplicationContext();
}