springboot @webservice 注解的类中使用@Autowired 进行bean注入,注入的bean调用时为空的问题

原 Webservice 接口暴露方式如下

		//webservice 接口暴露
		String address = "http://localhost:8088/MyWebService";
		//使用Endpoint类提供的publish方法发布WebService,发布时要保证使用的端口号没有被其他应用程序占用
		Endpoint.publish(address, new TestWebServiceImpl());
		System.out.println("发布webservice成功!");

出现 @webservice 注解的类中使用@Autowired 进行bean注入,注入的bean调用时为空的问题,无法调用service、dao等

解决方式如下,使用@Bean 方式进行配置

package com.web.webservice;

import javax.xml.ws.Endpoint;

import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
 * cxf配置
 */
@Configuration
public class CxfConfig {

    @Autowired
    private Bus bus;

    @Autowired
    private TestWebServiceImpl testWebService;

    @Bean
    public Endpoint endpoint(){
        EndpointImpl endpoint = new EndpointImpl(bus, testWebService);
        endpoint.publish("/MyWebService");
        return endpoint;
    }
}

默认服务在Host:port/services/*路径下 

发布了48 篇原创文章 · 获赞 34 · 访问量 18万+

猜你喜欢

转载自blog.csdn.net/u014481096/article/details/80847250