Get Spring MessageContext

丶灬走出姿态 提交于 2019-12-23 18:43:14

问题


How to get Spring MessageContext from Java class?

Should I use some @Resource or @Autowire to inject context var to class or, for instance, to use some global context SpringContext or some other in order to get MessageContext.

What is approach?


回答1:


I'd start by reading what the standard method of doing this is and try that:

@Resource WebServiceContext wsContext;

@WebMethod public String echoHello(String msg) {
    MessageContext context = wsContext.getMessageContext();

    ...
}

That's how to do it on the server side. On the client side, the request and response contexts are just simple maps that you retrieve from the service stub (which will implement BindingProvider even if you don't explicitly ask for it); they don't need the scope management that MessageContext adds.




回答2:


If I understand correctly You're using Spring Webflow with JSF and need to access Webflow MessageContext from within a JSF ActionListener?

If so You can always use the RequestContextHolder (watch out to use the one from Webflow, as Spring MVC has it's own!), but it uses ThreadLocal so it's not too elegant:

MessageContext messageContext = 
    RequestContextHolder.getRequestContext().getMessageContext();

You cannot use standard DI, as the MessageContext is created per each request by the FlowExecutor on every flow start or resume, using MessageSource.




回答3:


in your flow.xml add a messageContext like this:

   <transition on="search">
     <evaluate expression="service.search(service.id, messageContext)" result="scope" />
   </transition>`

in your Service.java you can use the messageContext now.

public List search(String serviceId, MessageContext messageContext) {       
  ...       
  messageContext.addMessage(
  ...
}


来源:https://stackoverflow.com/questions/6276118/get-spring-messagecontext

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!