Springboot:
package com.example.cluster.web.domain.service.callback.impl; import com.example.cluster.common.utils.JacksonUtils; import com.example.cluster.web.domain.service.callback.CallBack; import com.example.cluster.web.infrastructure.handler.RequestHolder; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Service; @Slf4j @Service @RequiredArgsConstructor(onConstructor = @__(@Autowired)) public class JobChangerService { private final ApplicationContext applicationContext; public void backupCallback(String callerName, String uniqId, boolean isSuccess){ if (StringUtils.isBlank(callerName)) { return; } com.example.cluster.web.domain.entity.callback.CallBack back = JacksonUtils.parse(callerName,com.example.cluster.web.domain.entity.callback.CallBack.class); CallBack callBack = (CallBack) applicationContext.getBean(StringUtils.uncapitalize(back.getKey())); log.info("into callback step,callback is {}",callBack); log.info("isSuccess is {}",isSuccess); log.info("JobChangerService backupCallback {}",JacksonUtils.toJson(RequestHolder.getCommonRequest())); if (isSuccess) { log.info("isSuccess"); try { callBack.onSuccess(uniqId); } catch (InterruptedException e) { throw new RuntimeException(e); } return; } callBack.onFailed(uniqId); } }tomcat1:
web.xml <listener> <listener-class>com.keji.dms.context.ContextLoaderListener</listener-class> </listener> import org.springframework.web.context.support.WebApplicationContextUtils; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; public class ContextLoaderListener implements ServletContextListener { private static Logger logger = LoggerFactory.getLogger(ContextLoaderListener.class); @SneakyThrows @Override public void contextInitialized(ServletContextEvent servletContextEvent) { ServletContext servletContext = servletContextEvent.getServletContext(); ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext); DmsContext.getInstance().setServletContext(servletContext); DmsContext.getInstance().setApplicationContext(applicationContext); PropertyContext propertyContext = (PropertyContext) applicationContext.getBean("propertyContext"); DmsContext.getInstance().setPropertyContext(propertyContext); .... }tomcat2:
这段代码是一个经典的 Spring 容器工具类,用来在非 Spring 管理的代码中静态获取 IoC 容器。
代码含义:实现了 ApplicationContextAware 接口,Spring 会在初始化该 Bean 时"回填"容器上下文拿到的 ApplicationContext 存到静态字段中,之后任何地方(包括 new 出来的普通对象、静态方法)都可以通过 SpringUtil.getApplicationContext() 获取容器,进而 getBean(…)
setApplicationContext 什么时候被调用?
由 Spring 容器自动回调,时机是:
容器启动 → 扫描并实例化该 Bean → 属性注入完成之后 → 调用 Aware 接口回调(invokeAwareInterfaces)→ Bean 初始化完成
也就是说,应用启动时 Spring 创建 SpringUtil 这个 Bean 的过程中,容器会主动调用 setApplicationContext(…) 并把自己传进来,业务代码中不需要手动调用。注意 getApplicationContext() 里做了非空校验,就是因为如果 Spring 没有完成回调,静态字段就是 null
package com.example.util; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationContextException; import org.springframework.stereotype.Component; @Component public class SpringUtil implements ApplicationContextAware { private static ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { SpringUtil.applicationContext = applicationContext; } public static ApplicationContext getApplicationContext() { if (applicationContext == null) { throw new ApplicationContextException("SpringUtil.applicationContext is null"); } return applicationContext; } }