I'm using decorator pattern in java ee 7 (glassfish 4).
I've this decorator
@Decorator
public class FooIndexer implements FooService {
@Inject
@Delegate
FooService fooService;
private Logger logger = Logger.getLogger(FooIndexer.class.getName());
//@Inject
//Indexer indexer;
@Override
public Foo create(Foo foo, boolean index) {
fooService.create(foo, index);
if (index) {
System.out.println("Im in");
}
return foo;
}
}
And this ejb class
@Stateless(name = "fooService")
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
@DeclareRoles({"ADMINISTRATOR", "USER"})
public class FooServiceImpl implements FooService {
@PersistenceContext(unitName = "foo")
private EntityManager em;
@Resource(lookup="java:comp/EJBContext")
private SessionContext ctx;
/** CRUD **/
@RolesAllowed("ADMINISTRATOR")
public Foo create(Foo foo, boolean index) {
Principal cp = ctx.getCallerPrincipal();
System.out.println(cp.getName());
em.persist(foo);
return foo;
}
}
When I use this decorator pattern, EntityManager in EJB is null (without decorator, everything goes fine). I supose is because of decorator use @Inject instead of @EJB annotation (@EJB annotation can't be used in @Decorator), and EntityManager is not being injected.
But, what can I do to get entitymanager will be injected using @decorator?
Thank you