Java Code Examples for org.springframework.web.context.support.WebApplicationContextUtils#getRequiredWebApplicationContext()

The following examples show how to use org.springframework.web.context.support.WebApplicationContextUtils#getRequiredWebApplicationContext() . 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: MyHandlerInterceptor.java    From erp-framework with MIT License 6 votes vote down vote up
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    if(erpUserService == null) {
        System.out.println("erpUserService is null!!!");
        BeanFactory factory = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext());
        erpUserService = (ErpUserService) factory.getBean("erpUserService");
    }
    ErpUser user = erpUserService.findUserByLoginName(MySysUser.loginName());
    if(user != null) {
        // 通过拦截器,将登录用户信息存储,便于登录后展示
        request.setAttribute("currentUser", user);
        // 将登录用户名写入MDC中,方便后面mybatis拦截器中对createby  updateby字段赋值
        MDC.put("userid", MySysUser.loginName());
        return true;
    }
    return false;
}
 
Example 2
Source File: OpenEntityManagerInViewFilter.java    From lams with GNU General Public License v2.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 3
Source File: OpenEntityManagerInViewFilter.java    From java-technology-stack 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 4
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 5
Source File: StartupServlet.java    From webcurator with Apache License 2.0 6 votes vote down vote up
/** @see javax.servlet.Servlet#init(javax.servlet.ServletConfig). */
  public void init() {
      WebApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
      ApplicationContextFactory.setWebApplicationContext(context);
      
      // Initialise the permission mapping strategy.
      PermissionMappingStrategy strategy = (PermissionMappingStrategy) context.getBean("permissionMappingStrategy");
      PermissionMappingStrategy.setStrategy(strategy);
      
      // Add the default SSLContext provider for SSL, used by the ldaps protocol
      try {
	Security.addProvider(SSLContext.getDefault().getProvider());
} catch (NoSuchAlgorithmException e) {
	log.warn("Error adding SSL Provider to startup servlet.", e);
}   
  }
 
Example 6
Source File: WebConfigurer.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
@Override
public void contextDestroyed(ServletContextEvent sce) {
    LOGGER.info("Destroying Web application");
    WebApplicationContext ac = WebApplicationContextUtils.getRequiredWebApplicationContext(sce.getServletContext());
    AnnotationConfigWebApplicationContext gwac = (AnnotationConfigWebApplicationContext) ac;
    gwac.close();
    LOGGER.debug("Web application destroyed");
}
 
Example 7
Source File: ContextLoaderTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testContextLoaderListenerWithProgrammaticInitializers() {
	MockServletContext sc = new MockServletContext("");
	sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM,
			"org/springframework/web/context/WEB-INF/ContextLoaderTests-acc-context.xml");
	ContextLoaderListener listener = new ContextLoaderListener();
	listener.setContextInitializers(new TestContextInitializer(), new TestWebContextInitializer());
	listener.contextInitialized(new ServletContextEvent(sc));
	WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
	TestBean testBean = wac.getBean(TestBean.class);
	assertThat(testBean.getName(), equalTo("testName"));
	assertThat(wac.getServletContext().getAttribute("initialized"), notNullValue());
}
 
Example 8
Source File: OpenSessionInViewFilter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Look up the SessionFactory 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 SessionFactory to use
 * @see #getSessionFactoryBeanName
 */
protected SessionFactory lookupSessionFactory() {
	if (logger.isDebugEnabled()) {
		logger.debug("Using SessionFactory '" + getSessionFactoryBeanName() + "' for OpenSessionInViewFilter");
	}
	WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
	return wac.getBean(getSessionFactoryBeanName(), SessionFactory.class);
}
 
Example 9
Source File: WebConfigurer.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
@Override
public void contextDestroyed(ServletContextEvent sce) {
  log.info("Destroying Web application");
  WebApplicationContext ac = WebApplicationContextUtils.getRequiredWebApplicationContext(sce.getServletContext());
  AnnotationConfigWebApplicationContext gwac = (AnnotationConfigWebApplicationContext) ac;
  gwac.close();
  log.debug("Web application destroyed");
}
 
Example 10
Source File: ContextLoaderTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testContextLoaderListenerWithLocalContextInitializers() {
	MockServletContext sc = new MockServletContext("");
	sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM,
			"org/springframework/web/context/WEB-INF/ContextLoaderTests-acc-context.xml");
	sc.addInitParameter(ContextLoader.CONTEXT_INITIALIZER_CLASSES_PARAM, StringUtils.arrayToCommaDelimitedString(
			new Object[] {TestContextInitializer.class.getName(), TestWebContextInitializer.class.getName()}));
	ContextLoaderListener listener = new ContextLoaderListener();
	listener.contextInitialized(new ServletContextEvent(sc));
	WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
	TestBean testBean = wac.getBean(TestBean.class);
	assertThat(testBean.getName(), equalTo("testName"));
	assertThat(wac.getServletContext().getAttribute("initialized"), notNullValue());
}
 
Example 11
Source File: ContextLoaderTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testContextLoaderListenerWithGlobalContextInitializers() {
	MockServletContext sc = new MockServletContext("");
	sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM,
			"org/springframework/web/context/WEB-INF/ContextLoaderTests-acc-context.xml");
	sc.addInitParameter(ContextLoader.GLOBAL_INITIALIZER_CLASSES_PARAM, StringUtils.arrayToCommaDelimitedString(
			new Object[] {TestContextInitializer.class.getName(), TestWebContextInitializer.class.getName()}));
	ContextLoaderListener listener = new ContextLoaderListener();
	listener.contextInitialized(new ServletContextEvent(sc));
	WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
	TestBean testBean = wac.getBean(TestBean.class);
	assertThat(testBean.getName(), equalTo("testName"));
	assertThat(wac.getServletContext().getAttribute("initialized"), notNullValue());
}
 
Example 12
Source File: WebConfigurer.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
@Override
public void contextDestroyed(ServletContextEvent sce) {
  log.info("Destroying Web application");
  WebApplicationContext ac = WebApplicationContextUtils.getRequiredWebApplicationContext(sce.getServletContext());
  AnnotationConfigWebApplicationContext gwac = (AnnotationConfigWebApplicationContext) ac;
  gwac.close();
  log.debug("Web application destroyed");
}
 
Example 13
Source File: LearningWebsocketServer.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private static ILeaderselectionService getLeaderService() {
if (leaderService == null) {
    WebApplicationContext wac = WebApplicationContextUtils
	    .getRequiredWebApplicationContext(SessionManager.getServletContext());
    leaderService = (ILeaderselectionService) wac.getBean("leaderselectionService");
}
return leaderService;
   }
 
Example 14
Source File: LearningWebsocketServer.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private static IScratchieService getScratchieService() {
if (scratchieService == null) {
    WebApplicationContext wac = WebApplicationContextUtils
	    .getRequiredWebApplicationContext(SessionManager.getServletContext());
    scratchieService = (IScratchieService) wac.getBean(ScratchieConstants.SCRATCHIE_SERVICE);
}
return scratchieService;
   }
 
Example 15
Source File: SecurityRequestPostProcessors.java    From maven-framework-project with MIT License 5 votes vote down vote up
private UsernamePasswordAuthenticationToken authentication(ServletContext servletContext) {
	ApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
	UserDetailsService  userDetailsService = userDetailsService(context);
	UserDetails userDetails = userDetailsService.loadUserByUsername(this.username);
	return new UsernamePasswordAuthenticationToken(
			userDetails, userDetails.getPassword(), userDetails.getAuthorities());
}
 
Example 16
Source File: OpenSessionInViewFilter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Look up the SessionFactory 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 SessionFactory to use
 * @see #getSessionFactoryBeanName
 */
protected SessionFactory lookupSessionFactory() {
	if (logger.isDebugEnabled()) {
		logger.debug("Using SessionFactory '" + getSessionFactoryBeanName() + "' for OpenSessionInViewFilter");
	}
	WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
	return wac.getBean(getSessionFactoryBeanName(), SessionFactory.class);
}
 
Example 17
Source File: AgnUtils.java    From openemm with GNU Affero General Public License v3.0 4 votes vote down vote up
public static WebApplicationContext getSpringContextFromRequest( HttpServletRequest request) {
	return WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext());
}
 
Example 18
Source File: ToolDownload.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
protected IToolContentFullHandler getToolContentHandler(String toolContentHandlerName) {
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
return (IToolContentFullHandler) wac.getBean(toolContentHandlerName);
   }
 
Example 19
Source File: AbstractContextAwareSingletonBeanDependentTag.java    From attic-rave with Apache License 2.0 4 votes vote down vote up
protected <E> E getBeanFromContext(Class<E> clazz ) throws JspException {
    ServletContext currentServletContext = pageContext.getServletContext();
    ApplicationContext springContext = WebApplicationContextUtils.getRequiredWebApplicationContext(currentServletContext);
    return springContext.getBean(clazz);
}
 
Example 20
Source File: NiFiTestServer.java    From nifi with Apache License 2.0 2 votes vote down vote up
/**
 * Convenience method to provide access to Spring beans accessible from the
 * web application context.
 *
 * @param <T> target cast
 * @param beanName name of the spring bean
 * @param clazz class of the spring bean
 * @return Spring bean with given name and class type
 *
 * @throws ClassCastException if the bean found cannot be cast to the given
 * class type
 */
public <T> T getSpringBean(String beanName, Class<T> clazz) {
    ServletContext servletContext = webappContext.getServletHandler().getServletContext();
    WebApplicationContext webApplicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
    return clazz.cast(webApplicationContext.getBean(beanName));
}