Attempting to instantiate bean with CDI from a running web application

I have a jar that contains @Stateless class defined as

 @Stateless 
 public class TestBean() {
     @Inject 
     AnotherBean bean2;
     public String getThis() {
      return bean2.getAString();   
     }
 }

      

A web application (with a dependency on this jar) running wildfly 10.1.0 wants to instantiate this bean and use its methods. The web application invocation method can be a rest endpoint (called by some other web application) or just a regular java method.

What's the best way to create a TestBean? I've tried several solutions, none of which work.

For example this

Creating Standalone CDI 2

and this one

Does CDI work for a regular Java application?

I am new to CDI and how does it work, I am wondering if this is well explained somewhere?

+3


source to share


1 answer


Not entirely clear what your question is, but if you have a JAX-RS resource, in a WAR file, then this should just work



@Path("/somePath")
@RequestScoped
public class SomeResource {
    @Inject
    private TestBean testBean;
    @GET
    public String doGet() {
        return testBean.getThis();
    }
}

      

0


source







All Articles