Wednesday, December 16, 2009

Spring, JSF & Hibernate Integration - Part I

This tutorial mentions Step-by-Step instructions for developing Spring, JSF & Hibernate integrated application. It totally has three parts:

I. Creating simple Spring application
II. Creating application involving Spring and JSF
III. Creating application involving Spring, JSF and Hibernate

The current tutorial deals with the first part of creating application having only Spring. After the application is developed, a sample console program has been created which shows the application in run.

The final netbeans project after completing Part I of the current tutorial has been uploaded here. Because of size limitations the third party jars inside the lib folder has been deleted in the uploaded zip file.

Requirements :: Netbeans 6.7.1 with Java 6 installed

Steps
====
1. Create a java web project using Java EE 5 technology with the Java Server Faces & Hibernate frameworks included. Spring Web MVC is not required as we will be using JSF for page navigation.

2. Import and add SpringFramework 2.5 library. Spring MVC library is NOT required

3. create applicationContext.xml in WEB-INF folder with following content

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
</beans>

4. Delete hibernate.cfg.xml inside default source package, as we will be configuring hibernate in spring applicationContext.xml file

5. Add below entries to web.xml

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

6. Create Link.java POJO in domain package with getters & setters for its attributes. Also override the toString() method which returns a string containing link details.

package com.linkedlinks.domain;

/**
*
* @author harish
*/
public class Link {
private Long linkId;
private String httpLink;
private String title;
private String description;

public Link(){

}

public Link(String title,String httpLink,String description){
this.title = title;
this.httpLink = httpLink;
this.description = description;
}

public Link(Long id,String title,String httpLink,String description){
this.linkId = id;
this.title = title;
this.httpLink = httpLink;
this.description = description;
}
/**
* @return the httpLink
*/
public String getHttpLink() {
return httpLink;
}

/**
* @param httpLink the httpLink to set
*/
public void setHttpLink(String httpLink) {
this.httpLink = httpLink;
}

/**
* @return the title
*/
public String getTitle() {
return title;
}

/**
* @param title the title to set
*/
public void setTitle(String title) {
this.title = title;
}

/**
* @return the Description
*/
public String getDescription() {
return description;
}

/**
* @param Description the Description to set
*/
public void setDescription(String Description) {
this.description = Description;
}

/**
* @return the linkId
*/
public Long getLinkId() {
return linkId;
}

/**
* @param linkId the linkId to set
*/
public void setLinkId(Long linkId) {
this.linkId = linkId;
}

public String toString(){
return linkId+" "+title+" "+httpLink+" "+description;
}

}


7. Create LinkDao.java interface in repository package declaring required DAO methods in it.

package com.linkedlinks.repository;

import com.linkedlinks.domain.Link;
import java.util.List;

/**
*
* @author harish
*/
public interface LinkDao {
public List<Link> getLinks();
public void addLink(Link link);
public void removeLink(Link link);
public void updateLink(Link link);
public Link getLink(Long linkId);
public Long getTotalLinksCount();
}


8. Create SimulateDao.java implementing LinkDao which returns a sample implementation of DAO.

package com.linkedlinks.repository;

import com.linkedlinks.domain.Link;
import java.util.ArrayList;
import java.util.List;

/**
*
* @author harish
*/
public class SimulateDao implements LinkDao {

private List<Link> links;
static Long linkSequence = 6L;

public SimulateDao() {
links = new ArrayList<Link>();
Long[] linkids = {1L, 2L, 3L, 4L, 5L};
String[] httpLinks = {"http://link1", "http://link2", "http://link3", "http://link4", "http://link5"};
String[] titles = {"title1", "title2", "title3", "title4", "title5"};
String[] descs = {"desc1", "desc2", "desc3", "desc4", "desc5"};
for (int i = 0; i < linkids.length; i++) {
Link link = new Link(linkids[i], titles[i], httpLinks[i], descs[i]);
links.add(link);
}
}

public List<Link> getLinks() {
return links;
}

public void addLink(Link link) {
if (link.getLinkId() == null || link.getLinkId() == 0) {
link.setLinkId(linkSequence++);
}
links.add(link);
}

public void removeLink(Link link) {
links.remove(link);
}

public void updateLink(Link link) {
removeLink(link);
addLink(link);
}

public Link getLink(Long linkId) {
for (int i = 0; i < links.size(); i++) {
Link link = links.get(i);
if (linkId.equals(link.getLinkId())) {
return link;
}
}
return null;
}

public Long getTotalLinksCount() {
return (long) links.size();
}
}


9. Create LinksService.java interface declaring the business requirement methods.

package com.linkedlinks.service;

import com.linkedlinks.domain.Link;
import java.util.List;

/**
*
* @author harish
*/
public interface LinksService {
public Long getTotalLinksCount();
public List<Link> getLinks();
public Link createLink(String title,String desc,String httpLink);
public void removeLink(Link link);
public void updateLink(Link link);
}


10. Create LinksServiceImpl.java implementing LinksService which accesses any of the DAO implementation classes for data access or storage

package com.linkedlinks.service;

import com.linkedlinks.domain.Link;
import com.linkedlinks.repository.LinkDao;
import java.util.List;

/**
*
* @author harish
*/
public class LinksServiceImpl implements LinksService {
private LinkDao linkDao;

public LinksServiceImpl(){
}

public void updateLink(Link link){
linkDao.updateLink(link);
}

public Long getTotalLinksCount(){
return linkDao.getTotalLinksCount();
}

public List<Link> getLinks(){
return linkDao.getLinks();
}

public Link createLink(String title,String httpLink,String desc){
Link link = new Link(title,httpLink,desc);
linkDao.addLink(link);
return link;
}

public void removeLink(Link link){
linkDao.removeLink(link);
}

public void setLinkDao(LinkDao linkDao) {
this.linkDao = linkDao;
}
}


11. Now configure the beans in applicationContext.xml file to set proper dependency injection.

<bean id="myDao" class="com.linkedlinks.repository.SimulateDao" />
<bean id="myService" class="com.linkedlinks.service.LinksServiceImpl">
<property name="linkDao">
<ref bean="myDao" />
</property>
</bean>


12. Create a sample console client applicatin which uses LinksService to perform business functions. Create it under client package - ConsoleClient.java

package com.linkedlinks.client;

import com.linkedlinks.domain.Link;
import com.linkedlinks.service.LinksService;
import java.util.List;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;

/**
*
* @author harish
*/
public class ConsoleClient {

private LinksService linksService;

public static void main(String[] args) {
new ConsoleClient().demo();
}

public void demo() {
setupBeans();
displayAllLinks(linksService.getLinks());

Link link6 = linksService.createLink("title6","http://link6","desc6");
Link link7 = linksService.createLink("title7","http://link7", "desc7");

System.out.println("Two links added.. Total links count = " + linksService.getTotalLinksCount());
linksService.removeLink(link6);
link7.setDescription("Big Desc 7");
linksService.updateLink(link7);
System.out.println("link 6 removed.. link 7 description updated..");
displayAllLinks(linksService.getLinks());
}

private void setupBeans() {
Resource res = new FileSystemResource("web/WEB-INF/applicationContext.xml");
BeanFactory factory = new XmlBeanFactory(res);
LinksService myService = (LinksService) factory.getBean("myService");
setLinksService(myService);
}

private void displayAllLinks(List<Link> links) {
System.out.println("All links details");
for (Link link : links) {
System.out.println(link);
}
}

public LinksService getLinksService() {
return linksService;
}

public void setLinksService(LinksService linksService) {
this.linksService = linksService;
}
}


13. Now you can see the output by running the console client.

This completes the spring part of the tutorial. To contiue further for integrating JSF with Spring, go through part II of the tutorial here.

6 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. hai harish, thank you for this tutorial. i wanna ask about instruction no.11. How to configure it in applicationContext? inside beanS or outside? or what. Thankyouu

    ReplyDelete