Automatic assembly of bean of study notes

Autowiring of beans

What is autowiring of beans?

If one class wants to call another class, then our previous method is to call it in <property /> from the called class. As shown in the figure:


With autowiring, you don't have to worry about it. Next, let's introduce autowiring beans.

First autowired beans need to call autowire in <bean> like <bean id="xx" class="xx" autowrie="">

There are 6 attributes in autowire, which arebyName , byType, constructor, autodetect, defualt, no.

Next, explain with the code one by one

First look at the two class files

class file called

package it.autowire;


public class Person { private String name; private Dog dog;// The type of this variable is the called class public Person() { } //When the parameter of autowire in the xml file is constructor, it will call The bean in the bad function whose type matches the type of the called bean public Person(Dog dog) { System.out.println("configured through the constructor"); this.dog=dog; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Dog getDog() { return dog; } public void setDog(Dog dog) { this.dog = dog; }
































}

The called class file:

package it.autowire;


public class Dog {
 private String name;


public String getName() {
return name;
}


public void setName(String name) {
this.name = name;
}
 

}

Test class:

package it.autowire;


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


import it.bean.assemble.AllKindsService;


public class Test {


public static void main(String[] args) {
// TODO 自动生成的方法存根
   ApplicationContext ac=new ClassPathXmlApplicationContext("it/autowire/beans.xml");
   Person per=(Person) ac.getBean("person");

   System.out.println(per.getName()+"'s dog is "+ per.getDog().getName() );//When using the red body method, you need to call the dog object

}


}

byName:

It will find the same object name as the variable of the type of the called class called in the calling class, and implement the matching, and complete the assembly (the variable dog of the Dog object in the person class matches the id="dog" in the xml file and transfer

xml file



byType:

Looking for a bean of the same type as the attribute, can't find it, can't install it, and find multiple exceptions. The type of the variable dog is Dog, so it matches the dog whose type is Dog in the xml file to complete the injection


constructor:

Find one or more beans that are consistent with the bean's construction parameters, and throw an exception if no or more than one is found. Assemble according to the type of parameter 

This type will change a bit in the class file

public Person(Dog dog) {
System.out.println("Configuration via constructor");
this.dog=dog;

}//Such a constructor is required, the type of its parameter is called, and the assignment is completed


autodetect:

When autowire is autodetect, it will be assembled according to construct, if not, it will be assembled by ByType

default:

default This needs to be specified in <beans defualt-autorwire="specified", after specifying, the value of autowire defaults to the value specified in defaulit-autowire

no:

not assembled


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325615341&siteId=291194637