SPRING CONSTRUCTOR INJECTION EXAMPLE WITH XML CONFIG
Consider the
below example Players bean
class which contains fullName, role and board attributes with default and
parameterized constructor and toString() method(for textual representation of
object).
Players.java
package com.TheTechMatin.Entity;
public class Players {
private String fullName;
private String role;
private String board;
public Players() {
}
public Players(String fullName, String role, String board) {
this.fullName = fullName;
this.role = role;
this.board = board;
}
@Override
public String toString() {
return "Players
[fullName=" + fullName + ", role=" + role + ", board=" + board + "]";
}
}
The Players
class has three attributes i.e., name, role and board and a default constructor
and a parameterized constructor (basic convention) . The toString()
method is used to represent textual form of the Players object.
The
applicationContext.xml fie is used to do the spring bean configuration. The
below xml file shows how to set/assign a value to bean property through spring constructor
injection.
applicationContext.java
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="player" class="com.TheTechMatin.Entity.Players">
<constructor-arg value="Sir Ravindra Jadeja" type="String"></constructor-arg>
<constructor-arg value="All Rounder"></constructor-arg>
<constructor-arg value="BCCI"></constructor-arg>
</bean>
</beans>
The id
attribute of the bean element is used to specify the bean name
and class attribute is used to specify the fully qualified class
name of the bean.
The <constructor-arg>
element which is the sub-element of <bean> will invokes the constructor
with matching number of argument and will inject the value of value attribute
to a particular attributes.
The type attribute is optional for String type of attribute as by default it will be injected to string type of attributes of bean class, but in case of other data type u have to specify the type of attribute for example for id of int type the syntax would be <constructor-arg value="10" type="int"></constructor-arg> then the matching argument constructor will be invoked.
The type attribute is optional for String type of attribute as by default it will be injected to string type of attributes of bean class, but in case of other data type u have to specify the type of attribute for example for id of int type the syntax would be <constructor-arg value="10" type="int"></constructor-arg> then the matching argument constructor will be invoked.
Here in
this example we set “Sir Ravindra
Jadeja” for fullName attribute of Players
class similarly “All Rounder” for role attribute and “BCCI” for board
attribute thru constructor injection.
Now
executing the TestApp.java to test the application.
TestApp.java
package com.TheTechMatin.Entity;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestApp {
public static void main(String[] args) {
//load configuration file
ClassPathXmlApplicationContext
context = new ClassPathXmlApplicationContext("applicationContext.xml");
//just
printing msg
System.out.println("Hello Welcome To Sping Constructor
Injection!");
//get
the bean from applicationContext.xml
Players player = context.getBean(Players.class);
//print the Players attributes value
System.out.println(player);
//close the context
context.close();
}
}
So when we
execute the main class the following output will be displayed on the console.
Hello Welcome To Sping Constructor Injection!
Players [fullName=Sir Ravindra Jadeja, role=All Rounder,
board=BCCI]
Tested On
Eclipse IDE 2019-06.
No comments