English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Spring Auto-Configuration

The auto wiring feature of the Spring framework allows you to implicitly inject object dependencies. It uses setter or constructor injection internally.

Auto wiring cannot be used to inject primitive values and strings. It is only suitable for references.

Advantages of auto wiring

It needs Less code, because we do not need to write code to explicitly inject dependencies.

Disadvantages of auto wiring

Without the control of the programmer.

It cannot be used for primitive values and string values. It can only be used for references.

Auto wiring patterns

There are many auto wiring patterns:

PatternDescription
noThis is the default auto wiring pattern. This means that there is no auto wiring by default.
byNameThe byName pattern injects object dependencies based on the name of the bean. In this case, the property name and bean name must be the same. It internally calls the setter method.
byTypeThe byType pattern injects object dependencies based on type. Therefore, the property name and bean name can be different. It internally calls the setter method.
constructorThe constructor pattern injects dependencies by calling the constructor of the class. It calls the constructor with a large number of parameters.
autodetectfrom Spring 3is not recommended to use at the beginning.

Auto wiring example

Let's see the simple code for auto wiring in Spring. You need to use the autowire attribute of the bean element to apply the auto wiring pattern.

<bean id="a" class="org.sssit.A" autowire="byName"></bean>

Let's take a look at a complete example of Spring auto wiring. To create this example, we created4files.

B.java A.java applicationContext.xml Test.java

B.java

This class only includes constructor and methods.

package org.sssit;
public class B {
B(){System.out.println("b is created");}
void print(){System.out.println("hello b");}
}

A.java

This class includes a reference to class B and constructor and methods.

package org.sssit;
public class A {
B b;
A(){System.out.println("a is created");}
public B getB() {
  return b;
}
public void setB(B b) {
  this.b = b;
}
void print(){System.out.println("hello a");}
void display(){
  print();
  b.print();
}
}

applicationContext.xml

<?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="b" class="org.sssit.B"></bean>
<bean id="a" class="org.sssit.A" autowire="byName"></bean>
</beans>

Test.java

This class gets Bean from applicationContext.xml file and calls the display method.

package org.sssit;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
  ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
  A a=context.getBean("a",A.class);
  a.display();
}
}

Output:

b is created
a is created
hello a
hello b

1)byName automatic assembly mode

Under the byName automatic assembly mode, the bean ID and reference name must be the same.

Use setter injection internally.

<bean id="b" class="org.sssit.B"></bean>
<bean id="a" class="org.sssit.A" autowire="byName"></bean>

However, if the bean name is changed, it will not inject dependencies.

Let's see how to change the bean name from b to b1code.

<bean id="b1" class="org.sssit.B"></bean>
<bean id="a" class="org.sssit.A" autowire="byName"></bean>

2)byType automatic assembly mode

Under the byType automatic assembly mode, the bean ID and reference name may be different. However, there can only be one bean of the same type.

Internally uses setter injection.

<bean id="b1" class="org.sssit.B"></bean>
<bean id="a" class="org.sssit.A" autowire="byType"></bean>

In this case, it can work normally because you have created an instance of type B. It doesn't matter, you can use a bean name different from the reference name.

But if you have multiple beans of the same type, it will not work and throw an exception.

Let's see that there are many B-type beans in the code.

<bean id="b1" class="org.sssit.B"></bean>
<bean id="b2" class="org.sssit.B"></bean>
<bean id="a" class="org.sssit.A" autowire="byName"></bean>

In this case, it will throw an exception.

3)Constructor Auto-Configuration Mode

In constructor auto-configuration mode, the Spring container injects dependencies through the most parameterized constructor.

If a class has3a constructor with zero parameters, one parameter, and two parameters, and then injects by calling the two-parameters constructor.

<bean id="b" class="org.sssit.B"></bean>
<bean id="a" class="org.sssit.A" autowire="constructor"></bean>

4)No Auto-Configuration Mode

In the absence of auto-configuration mode, the Spring container will not inject dependencies through auto-configuration.

<bean id="b" class="org.sssit.B"></bean>
<bean id="a" class="org.sssit.A" autowire="no"></bean>