Spring MVC – Constructor Injection Example


Constructor Injection

Spring IOC will inject the dependencies using the constructor.All the dependencies are declared in the constructor.
Here we show a simple student details example of Constructor-Injection. We have already published another article on the
Spring Constructor Injection. This example
provides more details.

Spring Framework Articles

also read:

Spring MVC Constructor Injection example

1.Modify the web.xml to configure the Dispatcher Servlet.

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

2.Create an dispatcher-servlet.xml file which contains all the configuration beans to handle the user requests.It handles the user request and dispatches to respective controllers. In this file we declare the bean called Student and we pass the respective constructor-arguments using constructor-arg attribute.

Below code shows how to pass the dependancy arguments to constructor using constructor-arg attribute.

<bean id="student" class="Student">
             <constructor-arg index="0" type="java.lang.String" value="Ganesh"/>
             <constructor-arg index="1" type="int" value="20"/>
             <constructor-arg index="2" type="java.lang.String" value="Computer Science"/>



<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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">

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
      <property name="urlMap">
<entry  key="/index.html">
<ref bean="student"/>


 <bean id="student" class="Student">
             <constructor-arg index="0" type="java.lang.String" value="Ganesh"/>
             <constructor-arg index="1" type="int" value="20"/>
             <constructor-arg index="2" type="java.lang.String" value="Computer Science"/>


     <bean id="viewResolver"
          p:suffix=".jsp" />

3.Create a Jsp file index.jsp to display the output.


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib  uri="http://www.springframework.org/tags/form" prefix="form" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Welcome to Spring Web MVC project</title>

   <body bgcolor="#DDDDDD">
<h2>Spring MVC Constructor Injection Example</h2>
        <table align="center" style="font-weight:bold;">
                <td>Student Name </td>
                <td>Age </td>
                <td>Branch </td>



4.Create a Java class file Student.java which extends AbstractController and contains three fields (name,age and branch) which will be injected from the Spring IOC using the constructor. Here this class acts like a controller.


public class Student extends AbstractController


    private String name;
    private int age;
    private String branch;

    public Student() {


    public Student(String name, int age, String branch)  {
        this.name = name;
        this.age = age;
        this.branch = branch;

    public int getAge() {
        return age;

    public void setAge(int age) {
        this.age = age;

    public String getBranch() {
        return branch;

    public void setBranch(String branch) {
        this.branch = branch;

    public String getName() {
        return name;

    public void setName(String name) {
        this.name = name;

    protected ModelAndView handleRequestInternal(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {

         ModelAndView mv=new ModelAndView();

        mv.addObject("age", getAge());
        mv.addObject("branch", getBranch());
        return mv;



5.Building and running the application


Access page:http://localhost:8080/ConstructorInjection/index.htm

also read:



About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.


  1. […] Spring HTML TRANSFORM Tag (<spring:transform>) Spring MVC – DispatcherServlet Example Spring MVC – Constructor Injection Example Spring MVC – Setter Injection Example Extending ViewResolver and Chaining ViewResolvers in […]

Speak Your Mind


Please support the site
By clicking any of these buttons you help our site to get better