Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@Inject fails to initialize object

I am using tomee server to run my javaEE app. I have written a filter which injects an object. However the object does not seem to get instantiated :

Following is my code :

Filter.java

@Priority(value = 2)
@Provider
@Singleton
public class Filter implements ContainerRequestFilter {


     @Inject
     private Faculty faculty;


     public void filter(ContainerRequestContext requestContext) {
         System.out.println("faculty name is :"+faculty.getFacultyName());
     }
}

Faculty.java

import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;

public class Faculty {

    private List<String> facultyMembers;
    private String facultyName;

    @PostConstruct
    public void initialize() {
        this.facultyMembers = new ArrayList<String>();
        facultyMembers.add("Ian Schultz");
        facultyMembers.add("Diane Reyes");
        facultyName = "Computer Science";
    }

    public List<String> getFacultyMembers() {
        return facultyMembers;
    }

    public String getFacultyName() {
        return facultyName;
    }
}

I am getting an NPE. Following is the stack trace :

java.lang.NullPointerException
at com.xyz.commerceapp.filters.Filter.filter(TransactionStartFilter.java:45)
at org.apache.cxf.jaxrs.utils.JAXRSUtils.runContainerRequestFilters(JAXRSUtils.java:1645)
at org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.processRequest(JAXRSInInterceptor.java:201)
at org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.handleMessage(JAXRSInInterceptor.java:77)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:308)
at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)

Here are the versions of the artifacts being used:

<openejb.version>5.0.0-SNAPSHOT</openejb.version>
<tomee.version>2.0.0-SNAPSHOT</tomee.version>

I have tried including the beans.xml in both META-INF and WEB-INF but I am still seeing the NPE.Can someone let me know what am I doing wrong ?

like image 639
Suraj Menon Avatar asked Nov 21 '22 10:11

Suraj Menon


1 Answers

The injection worked once I changed @Singleton to @ApplicationScoped.

like image 167
Suraj Menon Avatar answered Dec 30 '22 23:12

Suraj Menon