Talking more about Constructor Injection in Spring using XML file

In previous tutorial, I introduced with you all basic knowledges about Constructor Injection in Spring using XML file. In this tutorial, I will show more information about it to hep you can use it easily!

Let consider following example.

I have Student class:

and Clazz class.

As you see, class Clazz defined a constructor with 2 arguments, one is Student object and other one is a variable type String. These arguments are dependency objects, so how can we inject these objects into Clazz object using this constructor?

First of all, to easy understand, I will create an example Maven project:

Talking more about Constructor Injection in Spring using XML file

With Application class has following content:

OK, let’s get started.

To inject Student object into Clazz object, first we need initial Student object in the Spring container! Example, I want to initial Student object with name is “Khanh”, I will declare as below:

With this declaration, Spring will call to the constructor:

to initial Student object.

In this declaration, I used <constructor-arg> tag with the attribute value to assign the value to name variable. This <constructor-arg> is used to define for the arguments in the constructor of the objects in Spring container. It has a lot of attributes but there are 2 common attributes is value and ref.

The value attribute is used in-case the ingested value is a text, a number, a boolean value,… The ref attribute is used to refer to another bean that has been initialized in Spring container.

OK, we just done declaration Student object, now we will continue with Clazz object.

To declare Clazz object, we will use <constructor-arg> with 2 attributes ref and value in the order as below:

Result:

Talking more about Constructor Injection in Spring using XML file

If you want to declare the arguments in any orders, you can declare as below:

Value of index starts from 0.

Beside that, instead using <constructor-arg> tag, Spring also supports us another simple way to work with Constructor Injection. That is: using the namespace c, xmlns:c=”http://www.springframework.org/schema/c”.

This namespace allows us can declare constructor arguments of objects inside the <bean> tag as attributes. There is no XSD schema file for this namespace. To use it, you only need declare in the XML configuration file of Spring as below:

then using following syntax to inject the dependency objects.

If you use the name of arguments in constructor to inject the dependency objects, you should declare an attribute in <bean> tag begin with “c:”, next is the name of argument and end with “-ref” in-case the dependency object is other beans in Spring container, then assign for it a value. Example, I can declare Student object as below:

If you use the index to inject the dependency objects, let declare an attribute in <bean> tag begin with “c:_”, next the index and finally is “-ref” in-case the dependency object is other beans in Spring container, then assign it a value. Example, I can declare Clazz object as below:

Result:

Talking more about Constructor Injection in Spring using XML file

 

Reference project on GitHub: https://github.com/huongdanjavacom/huongdanjava.com/tree/master/spring-constructor-injection

Talking more about Constructor Injection in Spring using XML file
Đánh giá bài viết
Chia sẽ bài viết này ...Share on Facebook
Facebook
0Tweet about this on Twitter
Twitter
Share on LinkedIn
Linkedin

Add Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.