Spring MVC – Setter Injection Example

Setter Injection

Spring IOC container will inject the dependencies using the setter bean class.All the dependencies are declared as the instance variables of the bean class.Here we show a simple Book details example of Setter-Injection.

Spring Framework Articles

also read:

Spring MVC Setter 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 Book and we set the properties of the bean using property with corresponding name and value attributes.

Below code shows how to pass the dependancy arguments to the bean using property attribute.

 <bean id="book" class="Book">
        <property name="bookName" value="Spring in Action"/>
        <property name="price" value="31"/>
        <property name="author" value="Craig Walls, Ryan Breidenbach"/>


<?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.htm">
<ref bean="book"/>

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

    <bean id="book" class="Book">
        <property name="bookName" value="Spring in Action"/>
        <property name="price" value="31"/>
        <property name="author" value="Craig Walls, Ryan Breidenbach"/>


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 Setter Injection Example</h2>
       <table align="center" style="font-weight:bold;">
               <td>Book Name</td>

4. Create a Java class file Book.java which extends AbstractController and contains three fields (bookName,price and author) which will be injected from the Spring IOC using the setter methods of the bean. Here this class acts like a controller.


public class Book extends AbstractController {

    private String bookName;
    private int price;
    private String author;

    public Book() {

    protected ModelAndView handleRequestInternal(
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        ModelAndView mv=new ModelAndView();
        mv.addObject("price", getPrice());
        mv.addObject("author", getAuthor());
        return mv;
        //throw new UnsupportedOperationException("Not yet implemented");

    public String getAuthor() {
        return author;

    public void setAuthor(String author) {
        this.author = author;

    public String getBookName() {
        return bookName;

    public void setBookName(String bookName) {
        this.bookName = bookName;

    public int getPrice() {
        return price;

    public void setPrice(int price) {
        this.price = price;


5. Building and running the application

Access page: http://localhost:8080/SetterInjection/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. This is something, perhaps, that you would not do in real time…


  1. [...] MVC – DispatcherServlet ExampleSpring MVC – Constructor Injection ExampleSpring MVC – Setter Injection ExampleExtending ViewResolver and Chaining ViewResolvers in Spring MVCSpring with Apache VelocityThe Spring [...]

Speak Your Mind