Spring notes 6-value and the difference between ref

If you want to keep watching the notes about Spring5, the following is the portal
Spring5 introductory knowledge integration (continuous update)

Considering the complete type of learning knowledge, I think it is necessary to take out the difference between value and ref and talk about it separately, but to be honest, this thing is still very easy to distinguish, and it is very fast to learn!

The difference is briefly described above

value: when the object of the bean instance injects the object property through the setter method, the property does not belong to the bean-id reference
ref: when the object of the bean instance injects the object property through the setter method, the property belongs to the bean-id Reference (usually only when the type is injected into the attribute of the class)

Look at the following example to understand the description, and get ready for the pre-work
1. Create a template (any name)
Insert picture description here

2. Create beans.xml file under resource
Insert picture description here

3. Create a package on blue java, create User class and Friend class under the package
Insert picture description here

4. Create myTest class on green java and
Insert picture description here
import basic configuration information in beans.xml
Insert picture description here

Code:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       https://www.springframework.org/schema/beans/spring-beans.xsd">

</beans>

Write the following attributes in the User class
Insert picture description here

Code:

public class User {
    
    
    private Friend myFriend;
    private String name;

    public Friend getMyFriend() {
    
    
        return myFriend;
    }

    public void setMyFriend(Friend myFriend) {
    
    
        this.myFriend = myFriend;
    }

    public String getName() {
    
    
        return name;
    }

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

    @Override
    public String toString() {
    
    
        return "User{" +
                "myFriend=" + myFriend.toString() +
                ", name='" + name + '\'' +
                '}';
    }
}

Write the following properties under the Friend class
Insert picture description here

Code:

package com.ysj.study;

public class Friend {
    
    
    private String name;
    private int age;

    public String getName() {
    
    
        return name;
    }

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

    public int getAge() {
    
    
        return age;
    }

    public void setAge(int age) {
    
    
        this.age = age;
    }

    @Override
    public String toString() {
    
    
        return "Friend{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

I believe students who have learned this should know that if you instantiate an object through the id of the bean.
Insert picture description here
First of all, for the int and String properties, they can be injected directly, without instantiation, so you can inject the value through the value.
But for the myFriend property, it is an object, and the instantiation of the class must be handed over to the bean for assembly and instantiation. One of the bean-id in this figure is an object of the Freind class, called friend. So you can inject this value into the id object of myUser, but because friend is assembled in a bean, you cannot use value. To use ref (reference bean-id),
in fact, to summarize ,As long as the value you need to inject is bean-id, use ref, otherwise you can use value (in the case of some complex types that are not considered)

Write the following code under the myTest class
Insert picture description here

Code:

import com.ysj.study.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class myTest {
    
    
    public static void main(String[] args) {
    
    
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

        User myUser = (User) context.getBean("myUser");

        System.out.println(myUser.toString());
    }
}

Output result
Insert picture description here

Guess you like

Origin blog.csdn.net/YSJ367635984/article/details/113246291