0

Hi I'm trying to Inject an object in my code. But for some reason it will keep null.

The following things I tried; Adding a PostConstruct(which isn't called...) Removing other parts of CDI to just get the first Injection working. Also checked if CDI is enabled in Payara.

I added an beans.xml to meta-inf & web-inf

Shortend version of the code where I call the bean.

public class MovieFacade implements iMovieFacade {
    @Inject
    private iMovieDao md;
    @PostConstruct
    void init(){
        System.out.println(md);//I do this to test if the postConstruct is called
    }   
    public List<Movie> getAllMovie() {
        return md.getAllMovies();
    }
}

Shortend code of the class I try to call.

@ApplicationScoped
public class MovieDao implements iMovieDao {

private DataStoreMaker dataStoreMaker;
private DCM dcm;

    @PostConstruct
    private void onInit(){
        dataStoreMaker = new DataStoreMaker();
        dcm = new DCM(dataStoreMaker.movieDS());
    }
    public List<Movie> getAllMovies(){
        List<Movie> ml = dcm.find().asList();
        return ml;
    }
}

The interface

public interface iMovieDao {
    void newMovie(Movie movie);
    Movie getId(String id);
    List<Movie> getAllMovies();
    void editMovie(Movie movie);
}

1 Answers1

0

In Java EE your MovieFacade should be a bean to be managed by container (have lifecycle). To do this just add annotation of your choice, for example @Stateless.

@Stateless
public class MovieFacade implements iMovieFacade {
    @Inject
    private iMovieDao md;
    @PostConstruct
    void init(){
        System.out.println(md);//I do this to test if the postConstruct is called
    }   
    public List<Movie> getAllMovie() {
        return md.getAllMovies();
    }
}
S. Kadakov
  • 861
  • 1
  • 6
  • 15