Spring Bean Definition Inheritance | Spring Boot
Tags: springbean, springboot, inheritance
Published: 2021-04-28 17:52:32

https://grokonez.com/spring-framework/spring-bean-definition-inheritance-spring-boot

Spring Bean Definition Inheritance | Spring Boot

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.

<!--more-->
<h2>I. Demo Video</h2>
<iframe width="560" height="315" src="https://www.youtube.com/embed/AImHllyZS5k" frameborder="0" allowfullscreen></iframe>
<h2>II. Technology</h2>
- Java 1.8
- Maven 3.3.9
- Spring Tool Suite – Version 3.8.1.RELEASE
- Spring Boot: 1.4.0.RELEASE
<h2>III. Overview</h2>
<h3>1. Goal</h3>
<img src="https://grokonez.com/wp-content/uploads/2016/09/beandefinheritance-goal.jpg" alt="beandefinheritance-goal" width="700" height="247" class="alignnone size-full wp-image-1149" />
To build an application that runs with beans:
- <strong>customerService</strong> is bean <strong>parent</strong> of:
+ <strong>anotherCustomerService</strong>: bean without declaring new class (use the same class as customerService)
+ <strong>telephoneService</strong>: bean of new class, inherit property from <strong>customerService</strong> and add new property too.
- <strong>beanWelcomeTemplate</strong>: a parent bean as abstract template that child beans can inherit.
- <strong>consultationService</strong>: child bean of <strong>beanWelcomeTemplate</strong>
<h3>2. Project Structure</h3>
<img class="alignnone size-full wp-image-1135" src="https://grokonez.com/wp-content/uploads/2016/09/beandefinheritance-projectstructure.jpg" alt="beandefinheritance-projectstructure" width="650" height="257" />
<h3>3. Step to do</h3>
- Create Spring Boot project
- Create Services
- Create Spring Bean Configuration File
- Create a Web Controller
- Run Spring Boot Application &amp; Enjoy Result
<h2>IV. Practice</h2>
<h3>1. Create Spring Boot project</h3>
- Open <strong>Spring Tool Suite</strong>, on <strong>Menu</strong>, choose <strong>File</strong> -&gt; <strong>New </strong>-&gt; <strong>Spring Starter Project</strong>, then fill each field:
<img src="https://grokonez.com/wp-content/uploads/2016/09/beandefinheritance-projectstart.jpg" alt="beandefinheritance-projectstart" width="626" height="432" class="alignnone size-full wp-image-1136" />

Click <strong>Next</strong>, in <strong>Web</strong>: choose <strong>Web</strong>:
<img class="alignnone size-full wp-image-1091" src="https://grokonez.com/wp-content/uploads/2016/09/springaop-aspectj-configweb.jpg" alt="springaop-aspectj-configweb" width="688" height="95" />

Click <strong>Finish</strong>. Spring Boot project will be created successfully.
<h3>2. Create Services</h3>
Under package <strong>service</strong>, create class <strong>CustomerService</strong> and <strong>SpecificService</strong.

Content of <strong>CustomerService.java</strong>:
<pre><code class="language-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;
	}
}
</code></pre>

Content of <strong>SpecificService.java</strong>:

More at:

https://grokonez.com/spring-framework/spring-bean-definition-inheritance-spring-boot

Spring Bean Definition Inheritance | Spring Boot