JavaEE Tip #6 - Resources

The @Resources annotation makes it possible to define resources that you are going to lookup at runtime. The example describes a way to lookup a data source that is mentioned in the @Resources annotation on the class.

        @Resource(name="ShoppingCartDB", type=javax.sql.DataSource),
        @Resource(name="ShoppingCartMail", type=javax.mail.Session)
    public class ShoppingCartBean {

        public List getItems() {
            DataSource shoppingCartDB = (DataSource) ctx.lookup("ShoppingCartDB");
            Connection conn = shoppingCartDB.getConnection():
            return null;

Posted December 18, 2014