To facilitate test driven development(TDD), the EJB 3.0 specification allows you to use annotations to inject dependencies through annotations on fields or setter methods.
Instead of complicated XML ejb-refs or resource refs, you can use the @EJB and @Resource annotations to set the value of a field or to call a setter method within your session bean with anything registered within JNDI.
You can use the @EJB annotation to inject EJB references and @Resource to access datasources.
The example shows two ways to get access to the Calculator EJB.
One is:
@EJB private Calculator calculator;
or
private Calculator set; @EJB(beanName="org.jboss.tutorial.injection.bean.CalculatorBean") public void setCalculator(Calculator c) { set = c; }
When the ShoppingCartBean instance is created, the EJB container will
set the calculator field using the jndiName of that particular
referenced EJB.
Another example should be the message driven context inside an MDB.
@Resource
private MessageDrivenContext mdc;
private MessageDrivenContext mdc;
@Resource(mappedName="DefaultDS") private javax.sql.DataSource ds;
In JBoss, whenever the mappedName() attribute is specified (
@Resource, @EJB), jboss will use that as the GLOBAL jndi name to look it up.
The @Resource annotation is used to inject these singletons as well.
@Resource javax.ejb.SessionContext ctx; @Resource javax.ejb.TimerService timer; @Resource javax.ejb.UserTransaction ut;
@EJB and @Resource also create an entry within the JNDI ENC of the bean. So, the above @EJB injection
will create an entryfor the reference calculator beanver is
currently being refactored in this area,
so an EJB3 specific namespace is being used.

Nessun commento:
Posta un commento