继承SpringBeanAutowiringSupport后依然无法使用@Autowired
o.s.w.c.s.SpringBeanAutowiringSupport : Current WebApplicationContext is not available for processing of PortalApplication: Make sure this class gets constructed in a Spring web application. Proceeding without injection.
原因分析
jax-ws 中bean的生命周期管理是不受spring 控制的。所以需要显式继承 SpringBeanAutowiringSupport
来看下实现源码,非常巧妙
/**
* This constructor performs injection on this instance,
* based on the current web application context.
* <p>Intended for use as a base class.
* @see #processInjectionBasedOnCurrentContext
*/
public SpringBeanAutowiringSupport() {
processInjectionBasedOnCurrentContext(this);
}
/**
* 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.");
}
}
}
从网上搜索到的也基本上都解释了这个方法,但是,在spring boot
中是不起作用的。
因为 spring boot
的Servlet Context Initialization
略微不一样
SpringBootServletInitializer.startup()
使用了自定义的ContextLoaderListener
,
并没有把创建的rootAppContext
传给ContextLoader
。
所以JAX-WS endpoint 类初始化的时候, SpringBeanAutowiringSupport从ContextLoader.getCurrentWebApplicationContext()
取值总是null
,@Autowired当然就不能用了。
public abstract class SpringBootServletInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
// Logger initialization is deferred in case a ordered
// LogServletContextInitializer is being used
this.logger = LogFactory.getLog(getClass());
WebApplicationContext rootAppContext = createRootApplicationContext(
servletContext);
if (rootAppContext != null) {
servletContext.addListener(new ContextLoaderListener(rootAppContext) {
@Override
public void contextInitialized(ServletContextEvent event) {
// no-op because the application context is already initialized
}
});
}
else {
this.logger.debug("No ContextLoaderListener registered, as "
+ "createRootApplicationContext() did not "
+ "return an application context");
}
}
}
好了,找到了问题所在,就好解决了,思路如下。
- 初始化时把
WebApplicationContext
存下来 - 参考SpringBeanAutowiringSupport 把
WebApplicationContext
拿出来,并使用
WebApplicationContextLocator.java
@Configuration
public class WebApplicationContextLocator implements ServletContextInitializer {
private static WebApplicationContext webApplicationContext;
public static WebApplicationContext getCurrentWebApplicationContext() {
return webApplicationContext;
}
/**
* 在启动时将servletContext 获取出来,后面再读取二次使用。
* @param servletContext
* @throws ServletException
*/
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
}
}
SpringBootBeanAutowiringSupport.java
public abstract class SpringBootBeanAutowiringSupport {
private static final Log logger = LogFactory.getLog(SpringBootBeanAutowiringSupport.class);
/**
* This constructor performs injection on this instance,
* based on the current web application context.
* <p>Intended for use as a base class.
* @see #processInjectionBasedOnCurrentContext
*/
public SpringBootBeanAutowiringSupport() {
System.out.println("SpringBootBeanAutowiringSupport.SpringBootBeanAutowiringSupport");
processInjectionBasedOnCurrentContext(this);
}
/**
* 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 = WebApplicationContextLocator.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.");
}
}
}
}
结论
jax-ws 或类似的应用,直接 使用我们改造后的AutowiringSupport即可
extends SpringBootBeanAutowiringSupport