Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
  ) { ......
like image 208
Saimir Avatar asked Feb 28 '26 22:02

Saimir


1 Answers

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?

like image 117
kolossus Avatar answered Mar 03 '26 11:03

kolossus