spring definition and assembly bean detail

  • 2020-12-09 00:50:53
  • OfStack

Before reading this article, you should read IOC and AOP and Code Examples for a simple Understanding of Spring for IOC and AOP. Let's get down to business. This article describes how to define and load Java Bean in Spring.

The business scenario

Again, the example of a person driving a car. First, define one Car interface and two implementations of Benz and BMW, then define one Person class, which depends on the Car interface.


public interface Car {
  void go();
}

public class Benz implements Car {
  public void go() {
    System.out.println("benz go......");
  }
}

public class BMW implements Car {
  public void go() {
    System.out.println("bmw go......");
  }
}

public class Person {
  String name = "";

  Car car = null;
  public Car getCar() {
    return car;
  }

  public void setCar(Car car) {
    this.car = car;
  }

  public Person(String name) {
    this.name=name;
  }

  public void Drive(){
    System.out.println(name+" is driving ...");
    car.go();
  }
}

In the Person class, we can see that the car object is a dependent object of the class and needs to be injected into the Person class through the constructor. The above code has no sign of Spring at all. How does Spring inject

Add Spring dependencies

Many projects now use maven to manage dependencies, as did this project. I added 1 dependency node in ES37en.xml


<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-core</artifactId>
  <version>3.2.6.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-beans</artifactId>
  <version>3.2.6.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>3.2.6.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context-support</artifactId>
  <version>3.2.6.RELEASE</version>
</dependency>

Place these dependency nodes under the dependencies node of the ES43en.xml file, and eclipse will automatically download the relevant packages to the default location.

Define and assemble Bean manually

In the root directory of the project, create a new xml file named bean.xml, which reads as follows:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  <bean id="car" class="org.zdk.springProj1.BMW" />
  <bean id="tom" class="org.zdk.springProj1.Person">
    <constructor-arg value="Tom" />
    <property name="car" ref="car" />
  </bean>
</beans>

The above XML file first defines 1 id as bean of car, and 1 id as bean of tom, and car as the dependency of tom < property name="car" ref="car" / > Is manually assembled into the car attribute of tom.

The act of creating a collaboration between application objects is called assembly (wiring), and this is the essence of dependency injection.
Next we instantiate an application context object in the main method and get the tom node in bean


public class App 
{
  public static void main( String[] args )
  {
    ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
    Person tom=(Person) context.getBean("tom");
    tom.Drive();
  }
}

Run the program, and the output result is:


Tom is driving ...
bmw go......

Automatically.

Again, the act of creating a collaboration between application objects is called assembly (wiring) rather than instantiating an object. In the xml file above, I passed < property name="car" ref="car" / > The xml configuration file is bound to become more complex as applications evolve, and we are starting to lag behind in associating id with car using ref="car". Let me show you how Spring implements the automatic assembly of bean.

Type of auto assembly

There are four assembly strategies in Spring. To reduce the complexity of this article, I introduce two commonly used strategies, byName and byType. As the name implies, the byName method is to see if the name of the attribute is the same as id of bean. In this case, the Person class has a property named car. If the class or property is set to auto assemble and the assembly strategy is byName, Spring will go to bean of id as car (must be the same name). The byType method compares whether the type is the same. For this example, if the Person class or Person class has the car attribute set to autowater and the assembly policy is byType, car is of type Car, then Spring will go to bean of type Car or a subclass of bean when autowater.

XML is used for automatic assembly

Now I'll modify the code to use the xml configuration for automatic assembly.


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  <bean id="car" class="org.zdk.springProj1.BMW" />
  <bean id="tom" class="org.zdk.springProj1.Person" autowire="byName">
    <constructor-arg value="Tom" />
  </bean>
</beans>

The Person class does not require any changes.

First of all, I got rid of it < propertyname="car"ref="car"/ > This line of code, because it's manual. I then add one attribute autowire="byName" to tom's bean and set it to auto-assemble dependencies by name so that all Person's attribute names take on a special meaning when I get the Person object. Spring detected another attribute of Person class named car, and found one bean of id as car in the configuration file, so it was automatically assembled to Person's car attribute.

Automatic assembly through annotations

We modify ES161en. xml as follows


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans default-autowire="byName">
  <bean id="car" class="org.zdk.springProj1.BMW" />
  <bean id="tom" class="org.zdk.springProj1.Person">
    <constructor-arg value="Tom" />
  </bean>
</beans>

Delete one attribute of autowire="byName" of id as tom, and then add one attribute of ES172en-ES173en ="byName" to the node beans to set the default value of byName for all bean auto-assembly methods defined in this file. The Person class is then modified 1 bit by adding the @Autowired annotation to car's setter method to notify Spring to auto-assemble the property. Rerun the code, leaving the output unchanged.


public class Benz implements Car {
  public void go() {
    System.out.println("benz go......");
  }
}
0

Through annotations to realize automatic assembly, need to first set the default on beans the root directory of automatic assembly way, then you don't need in the configuration file for each one need to automatic assembly bean to configure autowire attributes (manner with the default if not 1, still can separate configuration, to override the default automatic assembly mode). The annotation is more subtle and provides attribute level control for bean that needs to be assembled automatically.

In all of the code above, we in the configuration file is used org. zdk. springProj1. BMW as Person class, if due to business needs, Person class don't need to open the BMW to drive Mercedes Benz, only need to amend the the configuration to org zdk. springProj1. Benz can, need not change any Java code, this is the power of Spring as IOC container.

conclusion

This is the full text of the spring definition and assembly of bean. I hope it will be helpful to you. Those who are interested can continue to see this site:

Java's Spring annotations configure bean instance code parsing

Details of the Bean life cycle used for Spring configuration

If there is any deficiency, please let me know. Thank you for your support!


Related articles: