Java and J2EE Tutorials, Jsp and Servlet Tutorials, Spring MVC, Solr, XML, JSON Examples, Hibernate & Struts 2 Hello World projects



Tuesday, 2 July 2013

Spring IOC - Using Spring DI with Setter Method, Constructor Argument passing and @autowired together

In our previous tutorials we came across what is Dependency Injection and how to implement it in Spring using Spring DI capabilities. We came across 'Spring DI using Setter Method' and 'Spring DI using constructor parameter passing'. In this particular blog we w'll come across how how to implement and use Spring DI with Setter Method, Constructor Argument passing and @annotation together in a single spring MVC application.


Project Structure


Before we start let's take a look at overall project structure for today's discussion. We have added three general purpose java classes to make it clear of Spring DI implementation. The three classes are used to show easy implementation of Spring IOC using Setter Method, Constructor argument passing and @annotations methods.


Libraries Used

Here is a list of all jar files used to implement Spring DI.


Using Spring DI with Setter Method, Constructor Argument passing and @autowired

Here is step by step configuration and implementation in details.

WebContent\WEB-INF\web.xml

This is an old web.xml file to tell the container that all upcoming requests are going to be handled by spring itself.
<?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>Spring3 IOC</display-name>
 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>

 <servlet>
  <servlet-name>spring</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>spring</servlet-name>
  <url-pattern>/</url-pattern>
 </servlet-mapping>
</web-app>


WebContent\WEB-INF\spring-servlet.xml

This is the core of all spring web applications, all spring based DI configurations and bean declaration goes here in a single file. This file shows what we need to add in order to implement and inject beans in spring container.
<?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:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-3.0.xsd">

 <!-- telling container to take care of annotations stuff -->
 <context:annotation-config />

 <!-- declaring base package -->
 <context:component-scan base-package="com.beingjavaguys" />

 <!-- adding view resolver to show jsp's on browser -->
 <bean id="viewResolver"
  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix" value="/WEB-INF/jsp/" />
  <property name="suffix" value=".jsp" />
 </bean>

 <!-- declare UserDao bean and injecting UserService's reference bean -->
 <bean id="userDao" class="com.beingjavaguys.dao.UserDaoImpl">
  <property name="userService" ref="userService" />
 </bean>

 <!-- declare UserService bean and injecting JdbcTemplate's reference bean >
 <bean id="userService" class="com.beingjavaguys.services.UserServiceImpl">
  <constructor-arg name="jdbcTemplate" ref="jdbcTemplate" />
 </bean>

 <!-- declare JdbcTemplate bean -->
 <bean id="jdbcTemplate" class="com.beingjavaguys.jdbc.JdbcTemplateImpl" />

</beans>


WebContent\index.jsp

This is a simple jsp file that is being used as welcome file, we have added a link here that w'll call the appropriate controller method to fire the actual stuff.
<html>
<head>
<title>Being Java Guys | Spring DI Hello World</title>
</head>
<body>
 <center>
   <b
   style="font-size: 20px; color: teal; font-family: monospace;">Being
   Java Guys | Spring IOC Hello World</b><br />
   <a href="hello">Click Here !</a>
 </center>
</body>
</html>


Spring DI using @autowired annotation


Here we have injected 'UserDaoImpl' class reference in Controller class. To do so all we need to do is adding a bean entry of injected class (i.e. UserDaoImpl) in SpringContext.xml and a reference variable of related interface in injecting class (i.e.HomePageController ) annotated with @autowired.

src\com\beingjavaguys\controller\HomePageController.java

This is simple spring controller, required method is annotated by @RequestMapping and the implementation code goes under the method.
package com.beingjavaguys.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.beingjavaguys.dao.UserDao;

@Controller
public class HomePageController {
 @Autowired
 UserDao userDao;

 @RequestMapping("/hello")
 public ModelAndView handleRequestInternal() {
  userDao.printData();
  System.out.println("From Controller");
  return new ModelAndView("hello");
 }
}


Spring DI using Setter Method


Here we have injected 'UserServiceImpl' class reference in 'UserDaoImpl' class. To do so all we need to do is adding a bean entry of injected class (i.e. UserServiceImpl) and passing this bean reference to Injecting class bean(i.e UserDaoImpl) as argument in SpringContext.xml . We than need to add a reference variable of related interface in injecting class (i.e.UserDaoImpl) along with a setter method.

src\com\beingjavaguys\dao\UserDao.java

This is a simple java interface having a simple test method, the implementation of the method goes under implemented class for the same.

package com.beingjavaguys.dao;

public interface UserDao {
public void printData();
}



src\com\beingjavaguys\dao\UserDaoImpl.java

This is a simple java class that is going to be used as a bean to demonstrate DI methodology in spring. Related interface's method is implemented here to print a simple text string on console. We will inject the reference of this bean in 'HomePageController' using @autowired annotation.
package com.beingjavaguys.dao;

import com.beingjavaguys.services.UserService;

public class UserDaoImpl implements UserDao {

 UserService userService;

 public void setUserService(UserService userService) {
  this.userService = userService;
 }

 @Override
 public void printData() {
  userService.printdata();
  System.out.println("From Dao");

 }

}



Spring DI using Constructor argument passing Method


Here we have injected 'JdbcTemplateImpl' class reference in 'UserServiceImpl' class. To do so all we need to do is adding a bean entry of injected class (i.e. JdbcTemplateImpl) and passing this bean reference to Injecting class bean(i.e UserServiceImpl) as constructor argument in SpringContext.xml . We than need to add a reference variable of related interface in injecting class (i.e.UserServiceImpl) and pass JdbcTemplateImpl as an argument in UserServiceImpl's constructor.

src\com\beingjavaguys\jdbc\JdbcTemplate.java

This is a simple java interface having a simple test method, the implementation of the method goes under implemented class for the same.
package com.beingjavaguys.jdbc;

public interface JdbcTemplate {
public void printdata();
}



src\com\beingjavaguys\jdbc\JdbcTemplateImpl.java

This is a simple java class that is going to be used as a bean to demonstrate DI methodology in spring. Related interface's method is implemented here to print a simple text string on console. We will inject the reference of this bean in 'UserServiceImpl' using constuctor argument passing method.
package com.beingjavaguys.jdbc;

public class JdbcTemplateImpl implements JdbcTemplate {

 @Override
 public void printdata() {
  System.out.println("From Template");

 }

}



src\com\beingjavaguys\services\UserService.java

This is a simple java interface having a simple test method, the implementation of the method goes under implemented class for the same.
package com.beingjavaguys.services;

public interface UserService {
 public void printdata();
}



src\com\beingjavaguys\services\UserServiceImpl.java

This is a simple java class that is going to be used as a bean to demonstrate DI methodology in spring. Related interface's method is implemented here to print a simple text string on console. We will inject the reference of this bean in 'UserDaoImpl' using setter method implementation.
package com.beingjavaguys.services;

import com.beingjavaguys.jdbc.JdbcTemplate;

public class UserServiceImpl implements UserService {

 JdbcTemplate jdbcTemplate;

 public UserServiceImpl(JdbcTemplate jdbcTemplate) {
  this.jdbcTemplate = jdbcTemplate;
 }

 @Override
 public void printdata() {
  jdbcTemplate.printdata();
  System.out.println("From Service");

 }

}



Output


If everything goes right and you did accurate configuration, you will see the hello.jsp file up in browser and a series of strings on console from different classes.

WebContent\WEB-INF\jsp\hello.jsp

<html>
<head>
<title>Being Java Guys | Spring DI Hello World</title>
</head>
<body>
 <center>
   <b
   style="font-size: 20px; color: teal; font-family: monospace;">This
   is all ! Happy Coding :)
 </center>
</body>
</html>


In this particular blog we came across how to implement Spring IOC - Using Spring DI with Setter Method, Constructor Argument passing and @annotation together in a single application. In upcoming blogs we will see more about Spring and other Java Technologies.







Thanks for reading !
Being Java Guys Team




1 comment:

  1. Thanks a lot!

    spring-servlet.xml line: 28 the comment ending should be -->

    ReplyDelete

Like Us on Facebook


Like Us On Google+



Contact

Email: neel4soft@gmail.com
Skype: neel4soft