简体   繁体   中英

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. 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 . 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.

  2. Just pull the bean from the plain servlet context using the following:

     //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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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