« Back

Using legacy DAOs with Liferay

General Blogs April 18, 2013 By Kan Zhang

Sten Martinez wrote a very nice article about Using a Legacy DB with Liferay Service Builder:

http://www.liferay.com/web/sten.martinez/blog/-/blogs/using-a-legacy-db-with-service-builder

It inspires me to start thinking: What if I want to use a Legacy DB WITHOUT Liferay Service Builder?

A lot of time when we have a legacy DB, most likely we will also have a legacy Spring/Hibernate based J2EE application. In the legacy Spring/Hibernate based J2EE application, you have all the DAOs implemented. How do I leverage these legacy DAOs in Liferay?

 

Prerequisites

Export the DAO classes in to a jar file and put them in container’s global lib folder.

 

1.Using legacy DAOs in a Spring managed class.

There are a many Class is managed by Spring. Such as the classes in a Spring MVC portlet, the Service Builder generated classes and most of the Liferay classes.

Using the legacy DAOs in a Spring managed class is straightforward. We can add the bean definitions for the legacy DAOs in the Spring context file and wire them with other Spring managed classes.

 

2.Using legacy DAOs in a Non-Spring managed class.

Some of the Classes are not managed by Spring in Liferay. Such as Liferay MVC portlet, ServicePreAction/ServicePostAction, LoginPreAction/LoginPostAction etc. We cannot directly inject the DAO dependencies into these classes.

We need to com.liferay.util.bean.PortletBeanLocatorUtil to load the legacy DAOs:

  1. Add the spring context file in web.xml:

<context-param>

                <param-name>portalContextConfigLocation</param-name>

                <param-value>/WEB-INF/classes/META-INF/dao-spring.xml</param-value>

</context-param>

  1. In the “dao-spring.xml” we can define the session factory and DAO beans and wire them with Controller/Service classes. Just like a normal Spring application.
  2. Use the com.liferay.util.bean.PortletBeanLocatorUtil to load the DAOs. For example, In the Liferay MVC portlet, if you configured <portlet-class>com.sample.mvc.SampleMVCPortlet</portlet-class> in portlet.xml and want to use legacy DAOs in the com.sample.mvc.SampleMVCPortlet:

public class SampleMVCPortlet extends MVCPortlet {

                private static SomeDAO someDAO;

                @Override

                public void doView(RenderRequest renderRequest,

                                                RenderResponse renderResponse) throws IOException, PortletException {

                               

                                SomeObject someObject = getSomeDAO().getSomeObject();

 

                                super.doView(renderRequest, renderResponse);

                }

    private SomeDAO getSomeDAO() {

        if (someDAO == null) {

                someDAO = (SomeDAO) PortletBeanLocatorUtil

                    .locate(SomeDAO.class.getName());

        }

        return someDAO;

    }

}

We can use the same approach in ServicePreAction, LoginPostAction etc.

No comments yet. Be the first.