0

We are facing weird injection issues in Widfly due to CDI changes. We have interface

public interface Command<I, O> {
}

and many classes implement this interface like this

public class ApproveUserRequests implements Command<ApproveUserRequestsRequest, List<String>> {
}

Application listener classes likes to get list of all classes available and uses injection like this

@Inject
private Instance<Command<I, O>> mActions;

However instance returned by mActions were always null. After debugging source found that the only way to get list of all instances is to use

@Inject
private Instance<Command<?, ?>> mActions;

Also we faced injection issues while using generic types , however using wildcard type helped us. - See more at: https://developer.jboss.org/thread/256783#sthash.1s6tuXsR.dpuf

Quality Catalyst
  • 6,531
  • 8
  • 38
  • 62
Srivathsan
  • 49
  • 1
  • 7
  • Please mark your code by either indenting with 4 spaces, or enclosing in back-ticks. That way you avoid issues with code coming out wrong and funny once posted. Also your two Injections look the same, the second one doesn't seem to do anything different? – YoYo May 05 '15 at 05:39
  • Noted. The second one is different, we are using wildcard lookups (?,?). first one uses . – Srivathsan May 05 '15 at 06:14
  • The wildcard makes sense that the previous version. Wildcard in generics means any type, so if you want all types, then inject using the anytype generics. I simply dont see anything weird here at all. Expected behaviour – maress May 05 '15 at 06:34
  • True, but that wildcard can get you any type, but I don't understand why injection of type Command does not work.There are many classes implementing Command. Note , this used to work in Jboss 7.1.1, that uses CDI spec 1.1 and older version of WELD. I am not sure what got changed in new version of WELD and CDI 1.2 in wildfly 8.2.0.Final – Srivathsan May 05 '15 at 07:21
  • Your question is lacking context: `Instance>` with unbound type variables won't even compile. – Harald Wellmann May 05 '15 at 07:50
  • public class ApplicationListener implements ServletContextListener { private static final String PREFIX = "com.collabnet.ctf.saturn.client.apps."; @Inject private Instance> mActions; – Srivathsan May 08 '15 at 10:25

1 Answers1

1

The rules for parameterized types have been clarified in CDI 1.2. Have look at Section 5.2.4 Assignability of raw and parameterized types of the spec.

Harald Wellmann
  • 12,615
  • 4
  • 41
  • 63
  • Could you add some code that fixes the issue above to your answer? – Jan Galinski May 05 '15 at 09:17
  • I think http://docs.jboss.org/cdi/spec/1.2/cdi-spec.html#assignable_parameters provides some information. In my case required type parameter are type variable (I) and (O) and bean type parameter are actual type ApproveUserRequestsRequest, List. According to the spec injection is not possible, does not satisfy any of the five conditions mentioned in section 5.2.4. Changing to wildcard tye "?", "?" does the trick. Note rawtypes in this case is Command class – Srivathsan May 08 '15 at 10:11