12.07.2015 Views

Beginning Java EE 6 with GlassFish 3, Second Edition

Beginning Java EE 6 with GlassFish 3, Second Edition

Beginning Java EE 6 with GlassFish 3, Second Edition

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 7 ■ SESSION BEANS AND THE TIMER SERVICEListing 7-12. The ejb-jar.xml FileItemEJBcom.apress.javaee6.ItemEJBcom.apress.javaee6.ItemLocalcom.apress.javaee6.ItemRemoteStatelessContaineraBookTitlejava.lang.String<strong>Beginning</strong> <strong>Java</strong> <strong>EE</strong> 6If the session bean is deployed in a jar file, the deployment descriptor needs to be stored in theMETA-INF/ejb-jar.xml file. If it’s deployed in a war file, it needs to be stored in the WEB-INF/ejb-jar.xmlfile. XML configuration is useful for details that are environment-specific and shouldn’t be specified inthe code through annotations (e.g., if an EJB needs to be run one way in development and another intest/production environments).Dependency InjectionI’ve already talked about dependency injection in this book, and you will come across this mechanismseveral times in the next chapters. It is a simple yet powerful mechanism used by <strong>Java</strong> <strong>EE</strong> 6 to injectreferences of resources into attributes. Instead of the application looking up resources in JNDI, they areinjected by the container.The containers can inject various types of resources into session beans using different annotations(or deployment descriptors):• @EJB: Injects a reference of the local, remote, or no-interface view of an EJB intothe annotated variable.• @PersistenceContext and @PersistenceUnit: Expresses a dependency on anEntityManager and on an EntityManagerFactory, respectively (see the discussion“Obtaining an Entity Manager” in Chapter 4).223

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!