I am using a decorator pattern in java ee 7 (glassfish 4).
I have 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; @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, the EntityManager in EJB is null (everything works fine without a decorator). I believe that using the @Inject decorator instead of the @EJB annotation (@EJB annotation cannot be used in @Decorator), and EntityManager is not introduced.
But, what can I do to get entitymanager to be injected using @decorator?
thanks
source share