• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer
  • Home
  • Java Tutorial
  • Node.js Tutorial
  • C Tutorial
  • Privacy Policy
  • Contact Us
  • About Us
Tech Stack Journal

Tech Stack Journal

  • Java
  • Node.js
  • Docker
  • Algorithms
  • Spring Core

Autowire Spring Beans using XML Configuration

August 29, 2020 by Admin Leave a Comment

Contents

  • 1 Class Structure
  • 2 Creation of Maven Project
  • 3 Creating the Model Class
  • 4 Data Access Layer
  • 5 Service Layer
  • 6 Main Application
  • 7 Spring Application Context XML
    • 7.1 Autowire by Constructor
    • 7.2 Autowire by Type
    • 7.3 Autowire by Name

Let’s look at the examples to Autowire the Spring beans by constructor, by type and by their names using Spring XML configuration.

Class Structure

  • Product – a POJO class to store product info
  • ProductDAO – an interface that we will use in a dependent service class
  • ProductDAOImpl – an implementation of ProductDAO returns list of mocked Product instances
  • ProductService – an interface that we will use in the application to hold a service instance
  • ProductServiceImpl – an implementation of ProductService and the dependent of ProductDAO instance
  • Application – It will get the ProductService instance to fetch products

Creation of Maven Project

First create a simple Maven project by skipping archtype selection in STS.

Add below pom.xml file which can be found at the root of the project.

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.techstackjournal</groupId>
	<artifactId>xml-config-autowire</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>4.3.2.RELEASE</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.2</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

Right click on the project, and select Maven=>Update Project. This will download all the required jar files for the application.

Creating the Model Class

Let’s create a Product model class which will hold the instances returned by the DAO layer.

package com.techstackjournal.model;

public class Product {

	private String productId;
	private String productName;
	private double price;

	public Product() {

	}

	public Product(String productId, String productName, double price) {
		super();
		this.productId = productId;
		this.productName = productName;
		this.price = price;
	}

	public String getProductId() {
		return productId;
	}

	public void setProductId(String productId) {
		this.productId = productId;
	}

	public String getProductName() {
		return productName;
	}

	public void setProductName(String productName) {
		this.productName = productName;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	@Override
	public String toString() {
		return "Product [productId=" + productId + ", productName=" + productName + ", price=" + price + "]";
	}

}

Data Access Layer

In this layer, we’ll create ProductDAO interface, which will be implemented by ProductDAOImpl. Also, ProductServiceImpl will use ProductDAO interface to call the findAll method. We call this approach as coding to interface. With this approach, we can decrease the dependence on the implementation class.

package com.techstackjournal.dao;

import java.util.List;

import com.techstackjournal.model.Product;

public interface ProductDAO {

	List<Product> findAll();

}

ProductDAOImpl implements ProductDAO and provides a findAll method. In this findAll method, we are not writing the actual data access logic to simplify the subject in focus. We’ll just mock the findAll method by returning a collection object.

package com.techstackjournal.dao;

import java.util.ArrayList;
import java.util.List;

import com.techstackjournal.model.Product;

public class ProductDAOImpl implements ProductDAO {
	public List<Product> findAll() {
		List<Product> products = new ArrayList<Product>();
		products.add(new Product("1", "Mobile", 400));
		products.add(new Product("2", "Book", 10));
		return products;
	}
}

Service Layer

In this Service Layer, we’ll be creating ProductService interface and ProductServiceImpl class.

package com.techstackjournal.service;

import java.util.List;

import com.techstackjournal.model.Product;

public interface ProductService {

	List<Product> findAll();

}

ProductServiceImpl implements ProductService interface. This class is in HAS-A relation with an object of type ProductDAO. But you can see that we are not creating any instance of type ProductDAO. Spring Container will wire the instance of ProductServiceImpl with the instance of type ProductDAO either by constructor or by type ProductDAO or by the name of the reference variable productDao.

package com.techstackjournal.service;

import java.util.List;

import com.techstackjournal.dao.ProductDAO;
import com.techstackjournal.model.Product;

public class ProductServiceImpl implements ProductService {

	private ProductDAO productDao;
	
	public ProductServiceImpl() {

	}

	public ProductServiceImpl(ProductDAO productDao) {
		super();
		this.productDao = productDao;
	}

	public List<Product> findAll() {
		return productDao.findAll();
	}

	public void setProductDao(ProductDAO productDao) {
		this.productDao = productDao;
	}

}

Main Application

In this class, we’ll be fetching the ProductService instance from Spring Container using ClassPathXmlApplicationContext.

package com.techstackjournal.app;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.techstackjournal.model.Product;
import com.techstackjournal.service.ProductService;

public class Application {

	public static void main(String[] args) {

		ApplicationContext appContext = new ClassPathXmlApplicationContext("appContext.xml");

		ProductService service = appContext.getBean("productService", ProductService.class);

		List<Product> products = service.findAll();

		for (Product product : products) {
			System.out.println(product);
		}

	}

}

Spring Application Context XML

Finally, in this appContext.xml file we’ll define 2 beans to get the instances of type ProductDAOImpl and ProductServiceImpl, respectively.

The location of the file should be under src/main/resources directory.

Autowire by Constructor

By defining the autowire attribute with value constructor, we can instruct Spring Container to inject ProductDAOImpl instance into ProductServiceImpl via its constructor.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean name="productDao" 
	      class="com.techstackjournal.dao.ProductDAOImpl" />

	<bean name="productService" 
		  class="com.techstackjournal.service.ProductServiceImpl" autowire="constructor" />
		  
</beans>

Autowire by Type

To instruct Spring Container to wire the beans by type, we initialize the autowire attribute with byType. This will match the available bean types in Spring context with the dependency requirements in ProductServiceImpl and wires them together automatically.

<bean name="productService" 
		  class="com.techstackjournal.service.ProductServiceImpl" autowire="byType" />

Autowire by Name

To instruct Spring container to wire the beans by names of the reference variables, we initialize the attribute autowire with byName value.

<bean name="productService" 
		  class="com.techstackjournal.service.ProductServiceImpl" autowire="byName" />
Next

Filed Under: Spring Core

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *


Primary Sidebar

  • Facebook
  • Pinterest
  • YouTube

More to See

How to Show Last Updated Date in Posts with Genesis Framework

December 12, 2020 By Admin

Get Environment Variables and System Properties in Java

September 18, 2020 By Admin

Tags

access modifiers in java Access Specifiers in Java array in class java ArrayIndexOutOfBoundsException Arrays in Java Constructor Definition in Java Constructor in Java Constructor in Java with Example Definition of Constructor in Java example of a loop Example of Constructor in Java final method final variable First Hello World Java Program for loop in java with example for loop java example for loops in java examples for loops java example How to run Java Hello World from command prompt How to use Constructor in Java How to write Hello World Java program in Eclipse Inheritance in Java Installing Java Installing Java in Windows Introduction to Java Introduction to Java Programming Iterating over an array Java Comments Java Do While Loop Java For Loop Java Hello World Command Line Java Hello World Example Java Hello World in Eclipse Java Hello World Program Java Hello World Tutorial Java Inheritance Java Installation Java Intro Java Introduction Java Statements Java While Loop Static Methods in Java Static Variables in Java Types of Comments What is a Constructor in Java

Secondary Sidebar

Categories

  • Algorithms
  • Blogging
  • Docker
  • Java
  • Node.js
  • Spring Core

Archives

  • January 2021 (1)
  • December 2020 (1)
  • September 2020 (2)
  • August 2020 (5)
  • July 2020 (4)
  • June 2020 (1)
  • May 2020 (4)
  • April 2020 (22)
  • November 2019 (3)
  • September 2019 (2)
  • August 2019 (6)

Footer

Navigation

  • Home
  • Java Tutorial
  • Node.js Tutorial
  • C Tutorial
  • Privacy Policy
  • Contact Us
  • About Us

My Other Sites

Spec Watchers

Speak New Language

Recent

  • [Solved] How to fix java.lang.InstantiationException
  • How to Show Last Updated Date in Posts with Genesis Framework
  • Get Environment Variables and System Properties in Java
  • Autowire using Java Based Spring Configuration
  • Java based Spring Configuration

Search

Tags

access modifiers in java Access Specifiers in Java array in class java ArrayIndexOutOfBoundsException Arrays in Java Constructor Definition in Java Constructor in Java Constructor in Java with Example Definition of Constructor in Java example of a loop Example of Constructor in Java final method final variable First Hello World Java Program for loop in java with example for loop java example for loops in java examples for loops java example How to run Java Hello World from command prompt How to use Constructor in Java How to write Hello World Java program in Eclipse Inheritance in Java Installing Java Installing Java in Windows Introduction to Java Introduction to Java Programming Iterating over an array Java Comments Java Do While Loop Java For Loop Java Hello World Command Line Java Hello World Example Java Hello World in Eclipse Java Hello World Program Java Hello World Tutorial Java Inheritance Java Installation Java Intro Java Introduction Java Statements Java While Loop Static Methods in Java Static Variables in Java Types of Comments What is a Constructor in Java

Copyright © 2021 · Tech Stack Journal · Log in