2012-09-30

CDI @Produces not working?

I deployed my application to JBoss AS 7.1.1.Final and realised that my @Produces annotations were not really producing... But they were working fine with Arquillian. It was my programming error.

This does not work

@ApplicationScoped
public class TrackMappersFactory {

    @EJB
    private AudiobookMapper audiobookMapper;
    @EJB
    private PodcastMapper podcastMapper;
    @EJB
    private SongMapper songMapper;
    @EJB
    private VideoMapper videoMapper;

    @Produces
    @AggregateMapper
    public Map<Class<? extends TrackTo>, TrackMapper<? extends TrackTo>> getAggregateTrackMapper() {
The reason why is simple, this class is not detected by the container because it's not an EJB. The solution is to make it a @Singleton bean instead.

This works

@Singleton
public class TrackMappersFactory {

    @EJB
    private AudiobookMapper audiobookMapper;
    @EJB
    private PodcastMapper podcastMapper;
    @EJB
    private SongMapper songMapper;
    @EJB
    private VideoMapper videoMapper;

    @Produces
    @AggregateMapper
    public Map<Class<? extends TrackTo>, TrackMapper<? extends TrackTo>> getAggregateTrackMapper() {
Here is the diff for this change.

1 comment:

  1. Great Article android based projects

    Java Training in Chennai

    Project Center in Chennai

    Java Training in Chennai

    projects for cse

    The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

    ReplyDelete