Home > OS >  Dependency injection with qualifiers with Jersey
Dependency injection with qualifiers with Jersey

Time:10-25

I have a Java web application running on Tomcat 9.

It uses the tools: Primefaces 10, JSF 2, CDI, EclipseLink.

This application is modularized as follows:

model
dao
service
managedBean
web

I created another modules

microservice
restful

where the "restful module" is the jsersey restful webservice.

I need to incorporate to this webservice the "service", "dao" and "model" modules.

The classes in the service and dao modules are invoked by the application through dependency injection (CDI)

I need my restful webservice to execute the dependency injections of these modules

I configured the dependency injections with concrete classes, classes implementing interfaces and classes implementing interfaces using generics.

public class MyApplicationBinder extends AbstractBinder {

    // mapping dependency injection with jersey
    @Override
    protected void configure() {
        //bind(MicroserviceDataImpl.class).to(MicroserviceDataImpl.class); // concrete class
        //bind(MicroserviceDataImpl.class).to(MicroserviceData.class); // class implementing interface
        bind(CaracteristicaMS.class).to(new TypeLiteral<Microservice<CaracteristicaDTO>>(){}); // classe implementing interface with generics
        bind(MotivoMS.class).to(new TypeLiteral<Microservice<MotivoDTO>>(){});
        bind(SequenceRuleImpl.class).to(SequenceRule.class);
    }
}

My resource class is:

@RequestScoped @Path("/caracteristica")
public class CaracteristicaRestful {

    @Inject
    private Microservice<CaracteristicaDTO> cms;

    // other attributes and methods
}

My microservice class is

@Dependent
public class CaracteristicaMS implements Microservice<CaracteristicaDTO>, Serializable {

    @Inject @DaoType(TipoClasse.CARACTERISTICA)
    private Dao<Caracteristica> cd;

    // others attributes and methods
}

My dao class is

@Dependent @Default @DaoType(value = TipoClasse.CARACTERISTICA)
public class CaracteristicaDaoImpl extends AbsDao<Caracteristica> implements Dao<Caracteristica>, Serializable {

    @NotNull @Inject @PersistenceUnitNameType(CADMAT_PU)
    EntityManagerFactory emf;

    // others attributes and methods
}

The qualifiers are:

@Qualifier @Retention(RetentionPolicy.RUNTIME) @Target({TYPE, METHOD, FIELD, PARAMETER})
public @interface DaoType {
    public TipoClasse value();   
}

@Qualifier @Retention(RetentionPolicy.RUNTIME) @Target({TYPE, METHOD, FIELD, PARAMETER})
public @interface PersistenceUnitNameType {
    public String value();   
}

But my modules use classes that implement interfaces using generics and qualifiers.

I need to configure the bind of these classes in MyApplicationBinder.class with the qualifier, but I don't know how.

Can anybody help me?

CodePudding user response:

I solved the problem adding a Qualifier.class, and declaring my "qualifiedBy(Annotation) class in there.

public class Qualifier {
    public static class CaracteristicaMicroservice extends AnnotationLiteral<MicroserviceType> implements MicroserviceType {
        @Override
        public TipoClasse value() {
            return TipoClasse.CARACTERISTICA;
        }
    }
}

and in MyApplicationBinder.class I declared the bind like this:

bind(CaracteristicaMS.class).to(new TypeLiteral<Microservice<CaracteristicaDTO>>(){}).qualifiedBy(new Qualifier.CaracteristicaMicroservice());
    
  • Related