Sunday, 15 September 2013

java ee - Dependency injection and @Produces annotation for two classes -



java ee - Dependency injection and @Produces annotation for two classes -

in example, there 2 injected classes, entitymanager , usertransaction. don't understand why usertransaction can straight injected without annotating first, entitymanager has defined , annotated @produces in separate class?

thanks in advance.

carmanager.java

public class carmanager { @inject private entitymanager em; @inject private usertransaction utx; private long id; private logger log = logger.getlogger(carmanager.class.getsimplename()); public void setid(long id) { this.id = id; } /** * returns either new instance or managed instance of {@link car}. * produced entity should dependent scoped avoid incompatible proxies between * jpa , cdi. */ @produces public auto getcar() { if (id == null) { log.info("returning new instance of car"); homecoming new car(); } log.info("finding instance of auto id " + id); homecoming em.find(car.class, id); } ... }

entitymanagerproducer.java

public class entitymanagerproducer { @produces @persistencecontext private entitymanager em; }

because usertransaction called predefined bean, entitymanager not needs have own producer. see 25.4 using predefined beans in cdi applications list of predefined beans.

java-ee cdi

No comments:

Post a Comment