Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inject an interface implementation? [duplicate]

I have the following interface

public interface IMyMapper<T> {}

and the implementation

public class MyMapper implements IMyMapper<MyClazz> {}

Now I want to inject the class MyMapper into a Stateless bean:

@Stateless
public class MyService {

    @Inject
    MyMapper mapper; //<-- does not work

    @Inject
    IMyMapper<MyClazz> mapper; //<-- also does not work
}

My problem is that the injection does not work. I get an exception:

org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies    for type MyMapper with qualifiers @Default
at injection point [BackedAnnotatedField] @Inject mypackage.MyService.mapper
at mypackage.MyService.mapper(MyService.java:0)

at org.jboss.weld.bootstrap.Validator.validateInjectionPointForDeploymentProblems(Validator.java:370)
at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:291)
at org.jboss.weld.bootstrap.Validator.validateGeneralBean(Validator.java:134)
at org.jboss.weld.bootstrap.Validator.validateRIBean(Validator.java:165)
at org.jboss.weld.bootstrap.Validator.validateBean(Validator.java:529)
at org.jboss.weld.bootstrap.Validator.validateBeans(Validator.java:515)
at org.jboss.weld.bootstrap.Validator.validateDeployment(Validator.java:490)
at org.jboss.weld.bootstrap.WeldStartup.validateBeans(WeldStartup.java:419)
at org.jboss.weld.bootstrap.WeldBootstrap.validateBeans(WeldBootstrap.java:90)
at org.glassfish.weld.WeldDeployer.event(WeldDeployer.java:225)
at org.glassfish.kernel.event.EventsImpl.send(EventsImpl.java:131)
at org.glassfish.internal.data.ApplicationInfo.load(ApplicationInfo.java:328)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:496)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:219)
at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:491)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:539)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:535)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Subject.java:360)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$2.execute(CommandRunnerImpl.java:534)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$3.run(CommandRunnerImpl.java:565)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$3.run(CommandRunnerImpl.java:557)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Subject.java:360)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:556)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1464)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$1300(CommandRunnerImpl.java:109)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1846)
at org.glassfish.deployment.autodeploy.AutoOperation.run(AutoOperation.java:164)
at org.glassfish.deployment.autodeploy.AutoDeployer.deploy(AutoDeployer.java:597)
at org.glassfish.deployment.autodeploy.AutoDeployer.deployAll(AutoDeployer.java:484)
at org.glassfish.deployment.autodeploy.AutoDeployer.run(AutoDeployer.java:412)
at org.glassfish.deployment.autodeploy.AutoDeployer.run(AutoDeployer.java:403)
at org.glassfish.deployment.autodeploy.AutoDeployService$1.run(AutoDeployService.java:233)
at java.util.TimerThread.mainLoop(Timer.java:555)
at java.util.TimerThread.run(Timer.java:505)
]]

How can I solve this?

like image 414
flash Avatar asked Sep 22 '14 20:09

flash


1 Answers

You probably are in annotated bean discovery mode (the default mode in CDI 1.1+), that's why your class MyMapper is not considered as a bean class. In this mode, to be discovered bean class should have a bean defining annotation. @Stateless is a bean defining annotation so your EJB is also a CDI bean but its injection point cannot be satisfied because MyMapper wasn't discovered a bean class.

To make your code work try to put @Dependent on your class

@Dependent    
public class MyMapper implements IMyMapper<MyClazz> {}

Alternatively you can change the discovery mode of your project as stated in the specification.

like image 139
Antoine Sabot-Durand Avatar answered Nov 17 '22 05:11

Antoine Sabot-Durand