/** * 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(); }
/** * 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; }
/** * 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; }
/** * 通过实体名称得它对应的service * * @param beanName */ @SuppressWarnings("unchecked") public static <T> T getBean(String beanName) { // 获取当前运行环境下Spring上下文 WebApplicationContext webApp = ContextLoader.getCurrentWebApplicationContext(); return (T) webApp.getBean(beanName); }
/** * 按类型获取spring bean 实例 * * @param beanType * @return */ public static <T> T getBean(Class<T> beanType) { // 获取当前运行环境下Spring上下文 WebApplicationContext webApp = ContextLoader.getCurrentWebApplicationContext(); return webApp.getBean(beanType); } }
webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
/** * 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."); } } }
@SuppressWarnings("unchecked") @Override public <T> T getEndpointInstance(Class<T> endpointClass) throws InstantiationException { WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext(); if (wac == null) { String message = "Failed to find the root WebApplicationContext. Was ContextLoaderListener not used?";
@Override public void onMessage(Message message) { String appCode = null; try { appCode = ((TextMessage) message).getText(); } catch (JMSException e) { logger.error("Jms illegal message!", e); } if (ssoAppCode.equals(appCode)) { // 1.失效所有session权限(session级别) PermissionFilter.invalidateSessionPermissions(); // 2.更新应用权限(Application级别) SmartContainer container = ContextLoader.getCurrentWebApplicationContext().getBean(SmartContainer.class); ApplicationPermission.initApplicationPermissions(container.getAuthenticationRpcService(), ssoAppCode); logger.info("成功通知appCode为:{}的应用更新权限!", appCode); } }
/** * 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."); } } }
public void init(FilterConfig config) throws ServletException { ConfigRepository configRepository = ContextLoader.getCurrentWebApplicationContext().getBean(ConfigRepository.class); FileResolver<HttpServletRequest> resolver = new FileResolver<>(); resolver.exportRepository("api/config-repository.git", configRepository.getGitRepo()); setRepositoryResolver(resolver); setReceivePackFactory(null); super.init(config); }
/** * 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(); }
public String getLocaleType() { Locale locale = LocaleContextHolder.getLocale(); MessageSource context = ContextLoader.getCurrentWebApplicationContext(); return context.getMessage("i18n.global.locale.type", null, locale); }
protected TenantResolver getTenantResolver() { final WebApplicationContext applicationContext = ContextLoader.getCurrentWebApplicationContext(); final TenantResolver tenantResolver = applicationContext.getBean(TenantResolver.class); if (tenantResolver == null) { throw new IllegalStateException("No TenantResolver found in application context!"); } return tenantResolver; }
@Override public void init() throws ServletException { super.init(); if (_delegate == null) { WebApplicationContext applicationContext = ContextLoader.getCurrentWebApplicationContext(); DescriptorService delegate = applicationContext.getBean(DescriptorService.class); if (delegate == null) { throw new ServletException("No delegate found in application context!"); } _delegate = delegate; } }
public static AutowireCapableBeanFactory getInjector() { if (injector == null) { // TODO when called during spring initialization, the application context is still null! injector = ContextLoader.getCurrentWebApplicationContext().getAutowireCapableBeanFactory(); } return injector; } }
public TaskRunner getDelegate() { if (_delegate == null) { WebApplicationContext applicationContext = ContextLoader.getCurrentWebApplicationContext(); if (applicationContext == null) { logger.warn("No WebApplicationContext available! Creating a temporary delegate!"); _delegate = createTemporaryDelegate(); } else { _delegate = applicationContext.getBean(TaskRunner.class); } } return _delegate; }
@Override public void init() throws ServletException { super.init(); if (_delegate == null) { WebApplicationContext applicationContext = ContextLoader.getCurrentWebApplicationContext(); ReferenceDataService delegate = applicationContext.getBean(ReferenceDataServiceImpl.class); if (delegate == null) { throw new ServletException("No delegate found in application context!"); } _delegate = delegate; } }
/** * 获取结果返回码对应语言的描述 * * @param result 结果返回码 * @return */ public static String getResultResource(IResultMessage result) { Locale locale = I18nTools.getLocale(); MessageSource context = ContextLoader.getCurrentWebApplicationContext(); return context.getMessage(result.getCode(), null, locale); }