【问题标题】:Get reference to spring bean from @WebService从 @WebService 获取对 spring bean 的引用
【发布时间】:2009-12-02 16:31:24
【问题描述】:

我正在使用 CXF 从 wsdl 生成 Web 服务。 生成的 Web 服务有注解 @WebService 如何从 Web 服务中获取对 Spring Bean 的引用? 我所有的spring bean都用@Service注释,我可以访问它们 在我的网络应用程序中。我如何也从我的 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?

有人有想法吗?

谢谢 阿隆

【问题讨论】:

    标签: java web-services spring cxf


    【解决方案1】:

    如果您使用的是 JUnit 4.4 或更高版本,您可以使用 Spring 2.5 JUnit 注释注入它。这是一个例子。

    http://hamletdarcy.blogspot.com/2008/12/autowired-junit-tests-with-spring-25.html

    当然,这种方法要求 Web 服务在测试开始时在 Web 上下文中运行,这是不言而喻的。你做到了吗?

    您也可能更喜欢使用 SOAP UI. 测试基于 WSDL 的服务

    【讨论】:

    • 我不想使用 JUnit 来完成这个任务,因为在我看来它不是解决它的标准方法。反正我用的是SOAP UI工具,确实很有帮助
    【解决方案2】:

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

    @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();
    

    }

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

    【讨论】:

      猜你喜欢
      • 2012-09-11
      • 2012-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-15
      • 2014-08-26
      • 2016-04-03
      • 1970-01-01
      相关资源
      最近更新 更多