7

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?

flash
  • 6,730
  • 7
  • 46
  • 70
  • Definitely not a dupe of that, but it has likely been answered before. – John Ament Sep 22 '14 at 23:12
  • 1
    No duplicate from my point of view. Of course, I've searched through SO before starting the question, but could not find a satisfying answer. – flash Sep 23 '14 at 06:16
  • I think @KumarAbhinav is correct (possible duplicate). CDI1.1 uses by default annotation to discover beans. Since the beans specified do not contain any scoped annotation, neither we are not told whether the beans.xml file is present. – maress Sep 23 '14 at 08:32
  • Could you provide you project structure? How is it packaged? Do you have a `BeanManger` declared? – tmarwen Sep 23 '14 at 09:12
  • 1
    Perhaps a duplicate but of which question? If I was looking for info on the subject and found this page, I'd be pissed off to see this duplicate mention with no link... – Antoine Sabot-Durand Sep 24 '14 at 12:46
  • @AntoineSabot-Durand There is the link to the duplicated question at the very top of the page: http://stackoverflow.com/questions/25488277/is-dependent-scope-not-default-in-wildfly – Benjamin Sep 25 '14 at 11:06

1 Answers1

9

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.

Antoine Sabot-Durand
  • 4,875
  • 17
  • 33