Quick to the point introduction to Spring Framework
Chapters
Bean Definition Inheritance
A definition of bean may contain the information of configuration, including values of property, constructor arguments, and also container information of specific like method of initialization etc.
A definition of Spring Bean inheritance concept is same as Java. A definition of child bean inherits data configuration from a base definition and the definition of child can add new values or override values if needed. Here a definition of base/parent bean is like template where child beans can inherit necessary configuration.
Example:
Java class: Home_Address
package pack1; public class Home_Address { private String Address1; public String setAddress1(String Address) { this.Address1 = Address; } public void getAddress1(){ System.out.println("Home Address1 : " + Address1); } }
Java class: Office_Address
package pack1; public class Office_Address { private String Address1; private String Address2; public String setAddress1(String Address) { this.Address1 = Address; } public String setAddress2(String Address) { this.Address2 = Address; } public void getAddress1(){ System.out.println("Office Address1 : " + Address1); } public void getAddress2(){ System.out.println("Office Address2 : " + Address2); } }
And our Test class: ExmTest
package pack1; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class ExmTest { public static void main(String[] args) { ApplicationContext cont = new ClassPathXmlApplicationContext("SingleBeans.xml "); Home_Address home =( Home_Address)cont.getBean("Home_Address"); home.getAddress1(); Office_Address office =( Office_Address )cont.getBean("Office_Address"); office.getAddress1(); office.getAddress2(); } }
The configuration file is SingleBeans.xml:
Here we defined "Home_Address
" bean which has one property Address1. Next "Office_Address
" bean is defined as a child of "Home_Address
" bean by using parent attribute. The child bean overrides Address1
property and added new Address2
property.
<?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-3.0.xsd"> <bean id="home" class="pack1.Home_Address"> <property name="Address1" value="National Blvd"/> </bean> <bean id="office" class="pack1.Office_Address" parent="home"> <property name="Address2" value="Motor Ave"/> </bean> </beans>
Here, we did not pass message2 while creating "office" bean, but it got passed because of Inheritance.
Compile and run the program will get the below result:
Result:
Home Address1 : National Blvd
Office Address1 : National Blvd
Office Address1 : Motor Ave
Bean Definition Template:
a Bean definition template can create by user where it can be used by bean child definitions without easily. For defining a Template of Bean Definition, we should not give class attribute and should give abstract attribute value as true:
<?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-3.0.xsd"> <bean id="homeTemplate" abstract="true"> <property name="Address1" value="National Blvd"/> </bean> <bean id="office" class="pack1.Office_Address" parent="homeTemplate"> <property name="Address2" value="Motor Ave"/> </bean> </beans>
Here the parent bean is marked as abstract hence it cannot be instantiated on its own because it is incomplete where abstract like this is usable only as a pure template definition of bean that serves as a definition of parent for definitions of child.
Description
This tutorial covers various topics releated to Spring Framework as listed below
- Framework Overview
- Modules
- Environment Setup
- Saying Hello World with Spring
- IoC Containers
- Bean Definition
- Bean Scopes
- Bean Life Cycle
- Bean Post Processors
- Bean Definition Inheritance
- Dependency Injection
- Injecting Inner Beans
- Injecting Collection
- Beans Auto-Wiring
- Annotation Based Configuration
- Java Based Configuration
- Event Handling in Spring
- Custom Events in Spring
- JDBC Framework Overview
- Transaction Management
Prerequisites
Prior knowledge of Java is essential
Audience
Beginners or students seeking quick introduction to Spring
Learning Objectives
This tutorial is for beginners seeking quick introduction to Spring Framework.
Author: Subject Coach
Added on: 22nd Jun 2015
You must be logged in as Student to ask a Question.
None just yet!