纪念一下,我在被这个问题折磨了3天之后,终于找到了解决方案!!!开心,万岁!!!
以下是我整理过后的。
网上很多都说让webService继承SpringBeanAutowiringSupport类就可以实现@WebService,但是Springboot项目行不通。
原因:
jax-ws 中bean的生命周期管理是不受spring 控制的。所以需要显式继承 SpringBeanAutowiringSupport。
但是spring boot 的Servlet Context Initialization 有些不一样SpringBootServletInitializer.startup() 使用了自定义的ContextLoaderListener,
并没有把创建的rootAppContext传给ContextLoader。
所以JAX-WS endpoint 类初始化的时候, SpringBeanAutowiringSupport从ContextLoader.getCurrentWebApplicationContext()取值总是null,@Autowired当然就不能用了。
以下是源码
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.");
}
}
}
解决思路:
1、初始化时把WebApplicationContext存下来
2、参考SpringBeanAutowiringSupport 把WebApplicationContext 拿出来,并使用
第一步:创建一个工具类,继承ServletContextInitializer类,重写里边的onStartup方法。
@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);
}
}
第二步:创建一个抽象类,作用等同于SpringBeanAutowiringSupport( 解决配置文件上下文和beanFactory工厂加载顺序导致autowire为空的异常),区别是获取到的WebApplicationContext是我们自己保存下来的。
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() {
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");
//注意此处要调用自己第一步创建的WebApplicationContextLocator
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.");
}
}
}
第三步:将@webService类继承SpringBeanAutowiringSupport类就可以了。
@WebService
@Service
public class OrgCheckWebServiceImpl extends SpringBootBeanAutowiringSupport implements IOrgCheckWebService {
@Autowired
public OrgCheckMapper orgCheckMapper;
@Autowired
public OrganizationMapper organizationMapper;
@Autowired
public IOrganizationService organizationService;
public static final Logger logger = LoggerFactory.getLogger(OrgCheckServiceImpl.class);
//方法体
}
结论
jax-ws 或类似的应用,直接使用改造后的AutowiringSupport即可
extends SpringBootBeanAutowiringSupport
出处:https://www.jianshu.com/p/125258ada53b