Define Student and Teacher classes for testing
- Student
public class Student {
private String name;
private Integer age;
public Student(a) {}public Student(Integer age, String name) {
this.age = age;
this.name = name;
}
public Integer getAge(a) {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName(a) {
return name;
}
public void setName(String name) {
this.name = name;
}
public void printStu(a) {
System.out.println("this is Student's method!"); }}Copy the code
- Teacher
public class Teacher {
private Student student;
public void print(a) {
System.out.println(student.getName() + ""+ student.getAge()); student.printStu(); }}Copy the code
First, the @autowired
- The XML configuration file is:
<bean id="teacher" class="spring5.Annotation.Teacher">
</bean>
<bean id="student" class="spring5.Annotation.Student">
<property name="name" value="nash"></property>
<property name="age" value="18"></property>
</bean>
Copy the code
1.1@Autowired’s automatic assembly method
Autowired
According to thebyType
The way to match inThe Ioc container
Look forBean
When there is only one Bean, Spring does property injection; otherwise, it throws an exception- Autowired is not assembled through the set function, so the Teacher class
You don't need setter methods
@Autowired
private Student student;
Copy the code
- Autowired annotations can be used not only on fields, but also on
Constructor, setter method
As used on a constructor:
@Autowired
public Teacher(Student student) {
this.student = student;
}
Copy the code
1.2 Running Results
Second, @ Qulifier
2.1 Usage of @qulifier
@Qulifier
Cooperate with@Autowired
Used when there are multiple beans of the same type
For example, two studentBeans are configured in the XML file:
<bean id="student1" class="spring5.Annotation.Student">
<property name="name" value="nash"></property>
<property name="age" value="18"></property>
</bean>
<bean id="student2" class="spring5.Annotation.Student">
<property name="name" value="dana"></property>
<property name="age" value="19"></property>
</bean>
Copy the code
In this case, @autoWired should be used in conjunction with @Qulifier
@Autowired
@Qualifier("student2")
private Student student;
Copy the code
2.2 Running Results
Third, @ the Resource
3.1 Automatic assembly of @Resource
- if
@Resource
If nothing follows, Spring presses by defaultbyName
To match. In terms ofbyName
If there are multiple beans of the same type, an exception will be thrown
<bean id="student" class="spring5.Annotation.Student">
<property name="name" value="nash"></property>
<property name="age" value="18"></property>
</bean>
Copy the code
@Resource
private Student student;
Copy the code
Running results:
- If I specify at sign Resource
name
ortype
Is matched according to the specified name or type
<bean id="stu" class="spring5.Annotation.Student">
<property name="name" value="nash"></property>
<property name="age" value="18"></property>
</bean>
Copy the code
@Resource(name = "stu")
private Student student;
Copy the code
Running results:
If type is specified, something like this:
@Resource(type = Student.class)
private Student student;
Copy the code
3.2 The difference between @autowired and @Resource
@Autowired
The default in accordance with thebyType
And the@Resource
The default in accordance with thebyName
matching@Autowired
是Spring
“, while@Resource
是J2EE
the
Fourth, @ the Value
- @value is not commonly used and is generally used to inject common types (e.g
String, int
Etc.) - There are three ways to fill in @Value
4.1@value (” “)
- Inject plain types directly
@Value("nash")
private String name;
@Value("1")
private int age;
@Value("https://www.csdn.net")
private Resource path;
Copy the code
4.2 @value (#{})
- said
SpEl expression
It is usually used to get a bean property, or to call a bean method, or to represent a constant - When a bean obtains a property of another bean via @value (” #{} “) or calls a method of another bean, as long as the bean(Beab_A) has access to the bean being called (Beab_B), that is, Beab_A and Beab_B are either in the same container, Or the container where Beab_B resides is the parent of the container where Beab_A resides.
@Value("#{T(java.lang.Math).random() * 100}")
private Integer age;
@Value("#{systemProperties['os.name']}")
private String osName;
@Value("#{1}")
private int number; // Get the number 1
Copy the code
See SpEl expressions for details
4.3 @value (${})
- It is used to obtain the attribute value of an external configuration file
Such as config. The properties
name = nash
Copy the code
@Value("${name}")
private String name;
Copy the code
reference
Spring @value annotation assignment