问题
I'm trying to inject two EJBs of same type with different annotation. But the instances injected are same.
@Path("/some")
public class SomeResource {
@Inject
@SomePostConstructionAnnotationForSomeService("this")
private SomeService s1;
@Inject
@SomePostConstructionAnnotationForSomeService("that")
private SomeService s2;
}
SomeService class looks like this.
public SomeService {
@PostConstruct
private void constructed() {
// find the annotation and do something else.
}
@Inject
private InjectionPoint injectionPoint;
}
The problem is that only s1
is invoked and s2
is equals s1
.
回答1:
This will work if SomeService
is CDI
bean and its scope is @Dependent
or no scope (that also means @Dependent
when injected into another bean). What scope does it have? If SomeService
is EJB
bean not CDI
this won't work.
UPDATE: You could move SomeService to a base class and create two different EJB beans by extending this class and inject these EJBs.
来源:https://stackoverflow.com/questions/32909962/how-can-i-inject-two-ejbs-of-same-type