@Stateless(name="personBean") @Local(PersonManager.class) public class PersonManagerBean implements PersonManager { @PersistenceContext(unitName="unit") private EntityManager em; @Resource private String defaultName; @Override public void save(Person person) { em.persist(new Person(defaultName)); } }
The defaultName is to be injected
In ejb-jar.xml
<enterprise-beans> <session> <ejb-name>personBean</ejb-name> <env-entry> <env-entry-name> session.PersonManagerBean/defaultName </env-entry-name> <env-entry-type>java.lang.String</env-entry-type> <env-entry-value>Tom</env-entry-value> </env-entry> </session> </enterprise-beans>
Now the defaultName is initialised with the value ‘Tom’.
The xml equivalence of @Resource annotation:
<enterprise-beans> <session> <ejb-name>personBean</ejb-name> <env-entry> <env-entry-name> session.PersonManagerBean/defaultName </env-entry-name> <env-entry-type>java.lang.String</env-entry-type> <env-entry-value>Tom</env-entry-value> <injection-target> <injection-target-class> session.PersonManagerBean </injection-target-class> <injection-target-name> defaultName </injection-target-name> </injection-target> </env-entry> </session> </enterprise-beans>