从cxf Web服务访问托管bean

时间:2013-04-11 15:08:53

标签: web-services jsf-2 cxf

@WebService(endpointInterface = "login")
public class LoginService{
 loginCredentials.login();
 }

public class LoginCredentials {

@ManagedProperty(name = "applicationBD", value = "#{applicationBD}")
private IApplication applicationBD; //This class is application scoped

    How to access applicationBD in this layer?  

     //facescontext is null while calling this service from SOAP UI
    ServletContext servletContext = (ServletContext)FacesContext.getCurrentInstance().getExternalContext()
            .getContext();
    servletContext.getAttribute("applicationStartupBD");
}

1 个答案:

答案 0 :(得分:0)

我不相信你想做什么是可取的,最好的设计。甚至合法:)。您可以从ServletContext

开始的链中访问WebServiceContext
  1. WebServiceContext的引用注入您的SIB

    @Resource
    WebServiceContext ctxt;
    
  2. WebServiceContext,检索MessageContext(它比以前的上下文更接近webservices的SOAP有效负载处理结构)。从此,您将获得ServletContext

    MessageContext msgContext = ctxt.getMessageContext();
    ServletContext servletContext = (ServletContext)msgContext.get(MessageContext.SERVLET_CONTEXT);
    IApplication app = (IApplication)  servletContext.getAttribute("applicationStartupBD"); //You can do whatever you please at this point
    
  3. 这是推荐的方法吗?不,你有依赖关系切换。 webapp应该依赖于web服务,而不是相反。 IMO,webapp更容易改变,是堆栈中的第一层。我建议你坚持两层之间松耦合