- Dependency Injection (DI):
A Software System , is comprises of many modules. To deliver services this system is dependent on its modules. Spring framework provides this functionality through Dependency Injection.
- Servlet Dependency Injection(SDI):
A servlet can also be injected with its dependencies.The benefits of servlet dependencies in j2ee project :-
- Faster development.
- less coupling among modules.
Demo on Guice Servlet :
- Download the jar file from this link :-
http://code.google.com/p/google-guice/downloads/list
- Add it in you java build path :-
The required jar files are
[guice-3.0.jar, guice-servlet-3.0.jar, javax.inject.jar, aopalliance.jar]
- About web.xml entries,
<?xml version="1.0" encoding="UTF-8" ?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>GuiceSevletDemo</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>guiceFilter</filter-name>
<filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>guiceFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>com.sandeep.guice.servlet.config.ServletConfig</listener-class>
</listener>
</web-app>
- Let’s create a Student Service which will give us a student list in return. The purpose of creating this service is , we can inject this service as dependency to our servlet.
Student.java
package com.sandeep.data.service;
import java.util.List;
import com.sandeep.data.valueobjects.Student;
public interface StudentService {
public List<Student> getStudents();
}
package com.sandeep.data.service;
import java.util.List;
import com.sandeep.data.valueobjects.Student;
public interface StudentService {
public List<Student> getStudents();
}
- StudentServiceImpl.java
package com.sandeep.data.service.impl;
import java.util.ArrayList;
import java.util.List;
import com.sandeep.data.service.StudentService;
import com.sandeep.data.valueobjects.Student;
public class StudentServiceImpl implements StudentService {
@Override
public List<Student> getStudents() {
List<Student> lists = new ArrayList<Student>();
for (int i = 0; i < 10; i++) {
Student student = new Student();
student.setName("sandeep");
student.setRollno("01234");
lists.add(student);
}
return lists;
}
}
- Creating a GUICE servlet configuration ServletConfig.java .
package com.sandeep.guice.servlet.config;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.servlet.GuiceServletContextListener;
import com.google.inject.servlet.ServletModule;
import com.sandeep.data.service.StudentService;
import com.sandeep.data.service.impl.StudentServiceImpl;
import com.sandeep.guice.servlet.MyFirstGuiceServlet;
public class ServletConfig extends GuiceServletContextListener {
@Override
protected Injector getInjector() {
return Guice.createInjector(new ServletModule(){
@Override
protected void configureServlets() {
serve("/*").with(MyFirstGuiceServlet.class);
bind(StudentService.class).to(StudentServiceImpl.class);
}
});
}
}
In above code we are binding the service with the servlet
- The actual servlet code MyFirstGuiceServlet.java ,
package com.sandeep.guice.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.sandeep.data.service.StudentService;
import com.sandeep.data.valueobjects.Student;
/**
* Servlet implementation class MyFirstGuiceServlet
*/
@Singleton
public class MyFirstGuiceServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private StudentService studentService;
/**
* @see HttpServlet#HttpServlet()
*/
@Inject
public MyFirstGuiceServlet(StudentService studentService) {
super();
this.studentService = studentService;
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
List<Student> students=studentService.getStudents(); StringBuilder html= new StringBuilder("<TABLE border='1'>");
for(Student student: students){
html.append("<TR>");
html.append("<TD>");
html.append(student.getName());
html.append("</TD>");
html.append("<TD>");
html.append(student.getRollno());
html.append("</TD>");
html.append("<TR>");
}
html.append("<TABLE>");
html.append("<div class='example-caption'>Servlet dependecy injection demo by Guice Servlet<div>");
out.write(html.toString());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
}
}
@Singleton,@inject these annotation is requiered to do the dependency injection.
- The output of the projects