Spring Bean Definition Inheritance | Spring Boot

spring-framework-spring-bean-definition-inheritance-feature-image

Bean Definition Inheritance is a good solution for defining new bean which inherited certain bean conveniently.

We will learn how to use XML configuration to create Inheritance from Bean with parent or abstract template.

I. Demo Video

II. Technology

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE
– Spring Boot: 1.4.0.RELEASE

III. Overview

1. Goal

beandefinheritance-goal
To build an application that runs with beans:
customerService is bean parent of:
+ anotherCustomerService: bean without declaring new class (use the same class as customerService)
+ telephoneService: bean of new class, inherit property from customerService and add new property too.
beanWelcomeTemplate: a parent bean as abstract template that child beans can inherit.
consultationService: child bean of beanWelcomeTemplate

2. Project Structure

beandefinheritance-projectstructure

3. Step to do

– Create Spring Boot project
– Create Services
– Create Spring Bean Configuration File
– Create a Web Controller
– Run Spring Boot Application & Enjoy Result

IV. Practice

1. Create Spring Boot project

– Open Spring Tool Suite, on Menu, choose File -> New -> Spring Starter Project, then fill each field:
beandefinheritance-projectstart

Click Next, in Web: choose Web:
springaop-aspectj-configweb

Click Finish. Spring Boot project will be created successfully.

2. Create Services

Under package service, create class CustomerService and SpecificServiceCustomerService.java:


public class CustomerService {
	private String welcomeMessage;
	private String customerName;

	public String getWelcomeMessage() {
		return welcomeMessage;
	}

	public void setWelcomeMessage(String welcomeMessage) {
		this.welcomeMessage = welcomeMessage;
	}

	public String getCustomerName() {
		return customerName;
	}

	public void setCustomerName(String customerName) {
		this.customerName = customerName;
	}

	public String sayHello() {
		return welcomeMessage + " " + customerName;
	}
}

Content of SpecificService.java:


public class SpecificService {
	private String welcomeMessage;
	private String customerName;
	private String serviceName;

	public String getWelcomeMessage() {
		return welcomeMessage;
	}

	public void setWelcomeMessage(String welcomeMessage) {
		this.welcomeMessage = welcomeMessage;
	}

	public String getCustomerName() {
		return customerName;
	}

	public void setCustomerName(String customerName) {
		this.customerName = customerName;
	}

	public String getServiceName() {
		return serviceName;
	}

	public void setServiceName(String serviceName) {
		this.serviceName = serviceName;
	}

	public String sayHello() {
		return welcomeMessage + " " + customerName + ". This is " + serviceName;
	}

	public String sayWelcome() {
		return welcomeMessage + serviceName;
	}
}

3. Create Spring Bean Configuration File

Under src/main/resources, create bean.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">

	<bean id="customerService"
		class="com.javasampleapproach.beaninheritance.service.CustomerService">
		<property name="welcomeMessage" value="Hello!" />
		<property name="customerName" value="Jack." />
	</bean>

	<bean id="anotherCustomerService"
		parent="customerService">
		<property name="welcomeMessage" value="Hi...!" />
	</bean>

	<bean id="telephoneService"
		class="com.javasampleapproach.beaninheritance.service.SpecificService"
		parent="customerService">
		<property name="welcomeMessage" value="Hello...!" />
		<property name="serviceName" value="Telephone Service." />
	</bean>

	<bean id="beanWelcomeTemplate" abstract="true">
		<property name="welcomeMessage" value="Hello! Welcome to " />
	</bean>

	<bean id="consultationService"
		class="com.javasampleapproach.beaninheritance.service.SpecificService"
		parent="beanWelcomeTemplate">
		<property name="serviceName" value="Consultation Service." />
	</bean>

</beans>

– We don’t define class for bean anotherCustomerService, so the class will be the same as parent class: CustomerService
– bean telephoneService inherits customerService, so it has the same value of property customerName as parent bean, but it has different value of welcomeMessage, and has new property serviceName.
beanWelcomeTemplate is a template with abstract attributes, so it doesn’t specify class, but we must set abstract=”true”.
consultationService gets property’s value from template parent.

4. Create a Web Controller

Under package controller, create class WebController.


@RestController
public class WebController {
	ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");

	CustomerService service = (CustomerService) context.getBean("customerService");
	CustomerService anotherService = (CustomerService) context.getBean("anotherCustomerService");
	SpecificService telephoneService = (SpecificService) context.getBean("telephoneService");
	SpecificService consulteService = (SpecificService) context.getBean("consultationService");

	@RequestMapping("/service")
	public String generalService() {
		return service.sayHello();
	}
	
	@RequestMapping("/anotherservice")
	public String anotherService() {
		return anotherService.sayHello();
	}
	
	@RequestMapping("/telephoneservice")
	public String telephoneService() {
		return telephoneService.sayHello();
	}
	
	@RequestMapping("/consultservice")
	public String consultationService() {
		return consulteService.sayWelcome();
	}
}

5. Run Spring Boot Application & Enjoy Result

– Config maven build:
clean install
– Run project with mode Spring Boot App
– Check results:

Request 1
http://localhost:8080/service
beandefinheritance-req1

Request 2
http://localhost:8080/anotherservice
beandefinheritance-req2

Request 3
http://localhost:8080/telephoneservice
beandefinheritance-req3

Request 4
http://localhost:8080/consultservice
beandefinheritance-req4

V. Source Code

SpringBean-Definition-Inheritance



By grokonez | September 6, 2016.

Last updated on April 28, 2021.



Related Posts


Got Something To Say:

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

*