Friday, 6 July 2007

Dependency injection is not possible from a Struts Action class

Thanks to Debu for pointing out that when trying to use dependency injection JAVA EE 5 support limits this only to managed classes such as EJBs, Interceptors, Servlets, etc. I was attempting to do this from a Struts Action class and was not able do so, now I know why.

See Debu's blog entry for more details:

In the end I was forced into using regular JNDI lookup as follows from JDeveloper / OC4J from a Struts Action class:

HttpSession session = request.getSession();
ResultPager bean = (ResultPager) session.getAttribute("pagerbean");

if (bean == null)
Context ctx = new InitialContext();
bean = (ResultPager) ctx.lookup("ResultPager");
bean.init(_pageSize, "AllObjectsRo.countAll", "AllObjectsRo.findAll");
session.setAttribute("pagerbean", bean);
catch (NamingException ex)
throw new ServletException(ex);


No comments: