繼承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
來看下實(shí)現(xiàn)源碼兴溜,非常巧妙
/**
* 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.");
}
}
}
從網(wǎng)上搜索到的也基本上都解釋了這個方法套腹,但是趋箩,在spring boot
中是不起作用的泰偿。
因?yàn)?spring boot
的Servlet Context Initialization
略微不一樣
SpringBootServletInitializer.startup()
使用了自定義的ContextLoaderListener
,
并沒有把創(chuàng)建的rootAppContext
傳給ContextLoader
俩垃。
所以JAX-WS endpoint 類初始化的時(shí)候沈撞, SpringBeanAutowiringSupport從ContextLoader.getCurrentWebApplicationContext()
取值總是null
,@Autowired當(dāng)然就不能用了慷荔。
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");
}
}
}
好了,找到了問題所在缠俺,就好解決了,思路如下显晶。
- 初始化時(shí)把
WebApplicationContext
存下來 - 參考SpringBeanAutowiringSupport 把
WebApplicationContext
拿出來,并使用
WebApplicationContextLocator.java
@Configuration
public class WebApplicationContextLocator implements ServletContextInitializer {
private static WebApplicationContext webApplicationContext;
public static WebApplicationContext getCurrentWebApplicationContext() {
return webApplicationContext;
}
/**
* 在啟動時(shí)將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.");
}
}
}
}
結(jié)論
jax-ws 或類似的應(yīng)用,直接 使用我們改造后的AutowiringSupport即可
extends SpringBootBeanAutowiringSupport