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.
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <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> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping> <session-config> <session-timeout> 30 </session-timeout> </session-config> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
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"/> </bean>
dispatcher-servlet.xml
<?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"> <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/> <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="urlMap"> <map> <entry key="/index.htm"> <ref bean="book"/> </entry> </map> </property> </bean> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" 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"/> </bean> </beans>
3. Create a Jsp file index.jsp to display the output.
index.jsp
<%@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" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Welcome to Spring Web MVC project</title> </head> <body bgcolor="#DDDDDD"> <h2>Spring MVC Setter Injection Example</h2> <table align="center" style="font-weight:bold;"> <tr> <td>Book Name</td> <td>${BookName}</td> </tr> <tr> <td>Price</td> <td>${price}</td> </tr> <tr> <td>Author</td> <td>${author}</td> </tr> </table> </body> </html>
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.
Book.java
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("BookName",getBookName()); 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
Output
Access page: http://localhost:8080/SetterInjection/index.htm
also read: