有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java从@WebService获取对springbean的引用

我正在使用CXF从wsdl生成web服务。 生成的web服务具有注释@WebService 如何从web服务获取对Springbean的引用? 我所有的SpringBean都用@Service注释,我可以访问它们 在我的web应用程序中。如何从web服务访问它们

我尝试了以下方法:

public class TestWSImpl implements TestWSSoap{

    @Resource
    public WebServiceContext wsContext;

    @Override
    public String getTest() {

        ServletContext servletContext= (ServletContext) wsContext.getMessageContext().get(MessageContext.SERVLET_CONTEXT);

        ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);

        return "Test";
    }
}

但是getWebApplicationContext方法返回null

当我用getRequiredWebApplicationContext替换getWebApplicationContext时 我收到一条错误消息:未找到WebApplicationContext:未注册ContextLoaderListener

有人有主意吗

谢谢 阿隆


共 (2) 个答案

  1. # 1 楼答案

    那是很久以前的事了。 我正在查看我的代码,我发现我使用了以下方法 这应该是春天的开始:

    @PostConstruct
    public void init(){
        JaxWsServerFactoryBean svrFactory = new JaxWsServerFactoryBean();
        svrFactory.setServiceClass(MyWebService.class);
        svrFactory.setAddress("http://address.com");
        svrFactory.setServiceBean(wsHandler);
        svrFactory.getInInterceptors().add(new LoggingInInterceptor());
        svrFactory.getOutInterceptors().add(new LoggingOutInterceptor());
        svrFactory.create();
    

    }

    如果它能解决你的问题,请告诉我