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.

No comments:

Post a Comment