This website uses cookies to ensure you get the best experience. Learn More.
Using legacy DAOs with Liferay
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?
Export the DAO classes in to a jar file and put them in container’s global lib folder.
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.
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:
<context-param>
<param-name>portalContextConfigLocation</param-name>
<param-value>/WEB-INF/classes/META-INF/dao-spring.xml</param-value>
</context-param>
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.