Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

How to use auto – wiring in Spring Framework

In this article, we define what is auto – wiring and why should we use this. Also we will see how we use auto – wiring in spring applications.

As we know, spring is used for objects loose coupling. This kind of coupling is made available using xml configuration and annotations usually. We can define this object configuration using manually or automatically. Spring provides a way to define objects configuration automatically, which is well known as Spring Auto – wiring.

Spring Auto – wiring in detail:

Spring helps to skip some of the configuration that should be handled automatically using auto wiring. There are three kind of Auto – Wiring:

  • By name,
  • By type,
  • And by Constructor.

By name defines that all the variable defined in the bean class will looked up and match with the bean names, then that reference will be passed to bean automatically.

By type defines a type per bean, we can’t apply auto wire by type on more that one bean of same type.

Constructors can also be applied to single type of the bean at a time and initialize values by constructor.

It will instantiate bean and pass it to the relevant bean. But the condition is “One bean per tpe”.

Syntax: autowire = “”

If there is no auto – wire defined, it will be turned off. It’s good to use auto – wire in small bean applications, because in large app, we have to guess, what is being done by spring.

If the name of the bean is same as bean class variable names, we can define auto – wiring by name. To define auto – wiring by name use attribute as: autowire with value “byName”.

This will check all the member variables of bean class and will look up for the bean definition into the configuration file by bean class variable name.

Ex. <bean name = ‘test’ id = ‘test’ class = ‘com.test.Test’ autowire = ‘byName’> </bean>

Now lets see auto – wiring by type. This kind of auto – wiring by type of class bean class variables of one type will be for the bean type. To define auto – wiring by type using attribute: autowire by value “byType”.

Ex. <bean name = ‘test’ id = ‘test’ class = ‘com.test.Test’ autowire = ‘byType’> </bean>

In this case, we can’t have multiple variables with the same type into the bean class.

The next kind of auto – wiring by constructor, auto – wire by constructor is done by constructor injection. We will need to have constructors instead of having setter() and getter() methods.

Ex. <bean name = ‘test’ id = ‘test’ class = ‘com.test.Test’ autowire = ‘constructor’> </bean>

Note: if we don’t use autowire attribute into our bean tag definition, it is auto – wire off. It is not the good way of using auto – wiring every time. It is the good way to use auto – wire, if there is small amount of bean to be configured. Because in the large amount of auto – wiring, it would be complex to manage, how bean is managed by spring in what way?

So, in this article, we will article, we will go with an example of auto – wiring by name. We use spring 3.05 version to work with spring framework and use following listed application to run this example:

  • Eclipse IDE (Eclipse Juno)
  • JDK environment (JDK7)
  • Spring jar files library (Spring 3.05)

Figure 1 shows list of required jar files as shown below:

Spring 3.05 Jar files library

Figure 1: Spring 3.05 Jar files library

Figure 1 list all the required jars for spring 3.05 and newer files.

Now list the directory structure created for spring autowiring application:

Directory Structure

Figure 2: Directory Structure

Figure 2 shows the list of directories and files created to run spring autowire application example. Now lets list all the files codes, configuring spring autowiring strategies.

Listing 1 list codes for a bean class named Point.java class as below:

Listing 1: Point.java

package spring;

public class Point {
	private int x, y;

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}
	
}

Listing 1 define a Point.java class that declares two integer type variables x and y with its getter() and setter() methods.

Now, Lets define next bean class TrianglePoint.java java class as below:

Listing 2: TrianglePoint.java

package spring;

public class TrianglePoint {
	private Point a;
	private Point b;
	private Point c;	
	
	public Point getA() {
		return a;
	}
	public void setA(Point a) {
		this.a = a;
	}
	public Point getB() {
		return b;
	}
	public void setB(Point b) {
		this.b = b;
	}
	public Point getC() {
		return c;
	}
	public void setC(Point c) {
		this.c = c;
	}
	
	public void draw(){
		System.out.println("Point A(" + getA().getX() + "," + getA().getY() + ")");
		System.out.println("Point A(" + getB().getX() + "," + getB().getY() + ")");
		System.out.println("Point A(" + getC().getX() + "," + getC().getY() + ")");
		System.out.println("\n\n");
	}
}

Listing 2 defines TrianglePoints.java class that declares three Point class instances with its getter() and setter() methods that represent three triangle points. This class also define a draw() method that list these triangle points into the console.

Now let’s defines a Shape.java java class files into Listing 3 as below:

Listing 3: Shape.java

package spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Shape {
	public static void main(String[]args){
		ApplicationContext context = new ClassPathXmlApplicationContext("springContext.xml");
		TrianglePoint obj = (TrianglePoint)context.getBean("triangle");
		obj.draw();
	}
}

Listing 3 define a Shape.java class that declares a main() methods. This main() method create an instance of ApplicationContext and loads “springContext.xml” file beans definition into the spring container. Using this application context instance, create an instance of “TrianglePoint” bean class by calling getBean() method and pass “triangle” bean name as a parameter.

Now, Define “springContext.xml” file into Listing 4 as below:

Listing 4: springContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
	<bean id = "triangle" class = "spring.TrianglePoint" autowire = "byName"/>
	
	<bean id = "a" class = "spring.Point">
		<property name="x" value = "0"/>
		<property name="y" value = "0"/>
	</bean>
	
	<bean id = "b" class = "spring.Point">
		<property name="x" value = "-20"/>
		<property name="y" value = "0"/>
	</bean>
	
	<bean id = "c" class = "spring.Point"> 
		<property name="x" value = "0"/>
		<property name="y" value = "20"/>
	</bean>
</beans>

Listing 4 defines four <bean> tag definition into the single <beans> tag. First <bean> tag define a bean definition for “TrianglePoint” bean class with auto wiring feature by name and other three <bean> tag definitions define “Point” bean class definition with different values of its class member variables “x” and “y”.

If we use auto wiring by type, then we will be required to define three different type of class member variables into the bean class. But we are using three class member variable of the same type which are of “Point” class type.

If we use auto wiring by constructor, we will be required to define three separate constructor for these variable initialization and we will be using constructor injection instead of setter injection into spring configuration file.

Listing 5: Auto wiring by constructor

<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
	<bean id = "triangle" class = "spring.TrianglePoint" autowire = "constructor"/>
	
	<bean id = "a" class = "spring.Point">
	    <constructor-arg type = "Point" value= "0"/> 
	</bean>
	
	<bean id = "b" class = "spring.Point">
	    <constructor-arg type = "Point" value= "2"/>
	</bean>
</beans>

To run this example, right click on the “Shape.java” class file and choose Run As and choose “Run Java Application”. This will display three triangle coordinates into the console as shown into the figure 3:

Three Triangle Coordinates into the console

Figure 3: Three Triangle Coordinates into the console

Figure 3 shows the all three triangle coordinates that reads values from springContext.xml configuration files configured into the three different beans for Point.java. While autowiring is applied on “triangle” bean, it looks for all the class member variables and look for bean definition against the names. These beans initialize “Point” class member variables.

There are three bean definitions that refer to “Point” class and initialize its class member variables “x” and “y” with following values: (0,0), (-20,0) and (0,20).

Conclusion:

In this article, we learn about the need of autowiring in spring and its its ways of configuration in spring. We went through an examples that define the way of using auto wiring by names. Auto wiring is very useful sometimes. But, It should be avoided as much as possible in the large application, bBecause it is tedious task to manage spring auto – wiring. Actually, iIt becomes very complex to understand the flow of configuration while it’s doing auto wiring and configuring automatically.



Have more than 5 years experience about Java, .Net, C and C++.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login