简体   繁体   English

如何在其余的Web服务java上注入Application scope bean

[英]how to inject Application scope bean on rest web service java

I have to inject initApplicationContext bean on ContextCacheRefresh web service, but unsuccessful, initApplicationContext value always is null. 我必须在ContextCacheRefresh Web服务上注入initApplicationContext bean,但是不成功的initApplicationContext值总是为null。 Have anybody any idea haw to deal with it? 有没有人知道如何处理它?

@ManagedBean(name = "initApplicationContext", eager = true)
@ApplicationScoped
   public class InitApplicationContext {
             .......................
               }

and web service 和网络服务

  @Path("/serviceContext")
  public class ContextCacheRefresh  {

   @ManagedProperty(value = "#{initApplicationContext}")
    private  InitApplicationContext initApplicationContext;

  @GET
  @Path("/refreshContext")

 public Response refreshUserListOn(@QueryParam("param") String param
  ) { ......

You'll not be able to get JSF to inject resources into a non-JSF context, using @ManagedProperty . 使用@ManagedProperty ,您将无法让JSF将资源注入非JSF上下文。 Your options are 你的选择是

  1. Convert your managed bean to use CDI annotations ( @Named to declare the managed bean and @Inject instead of the JSF annotations you're working with now. 转换托管bean以使用CDI注释( @Named来声明托管bean和@Inject而不是现在正在使用的JSF注释。

  2. Just pull the bean from the plain servlet context using the following: 只需使用以下命令从普通的servlet上下文中提取bean:

     //inject the servlet context @javax.ws.rs.core.Context ServletContext servletContext public InitApplicationContext getInitContext(){ return (InitApplicationContext)servletContext.getAttribute("initApplicationContext"); } 

What you're working toward seems a bit dodgy to me. 你正在努力的事情对我来说似乎有点狡猾。 Why is your web application concerned with your RESTful endpoint to begin with? 为什么您的Web应用程序首先关注您的RESTful端点?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM