Spring annotation @Autowired and @Resource

The difference between @Autowired and @Resource

Test preparation

Create Student, School, and PrimarySchool classes and use annotations to assign basic data types

//Student引用school
public class Student {
    
    
    private School school;
    @Value("张三")
    private String sname;
    @Value("22")
    private int  sage;
    public School getSchool() {
    
    
        return school;
    }
    public void setSchool(School school) {
    
    
        this.school = school;
    }
    public String getSname() {
    
    
        return sname;
    }
    public void setSname(String sname) {
    
    
        this.sname = sname;
    }
    public int getSage() {
    
    
        return sage;
    }
    public void setSage(int sage) {
    
    
        this.sage = sage;
    }
    @Override
    public String toString() {
    
    
        return "Student{" +
                "school=" + school +
                ", sname='" + sname + '\'' +
                ", sage=" + sage +
                '}';
    }
}
public class School {
    
    
    @Value("北京大学附属一中")
   private String name;
    @Value("私立小学")
   private String type;
    public String getName() {
    
    
        return name;
    }
    public void setName(String name) {
    
    
        this.name = name;
    }
    public String getType() {
    
    
        return type;
    }
    public void setType(String type) {
    
    
        this.type = type;
    }
    @Override
    public String toString() {
    
    
        return "School{" +
                "name='" + name + '\'' +
                ", type='" + type + '\'' +
                '}';
    }
}
//PrimarySchool是School 的子类 
public class PrimarySchool extends School {
    
    
}

Scan package configuration in spring configuration file

 <!-- 扫描注解,这样com.liu包下的文件都能被扫描 -->
    <context:component-scan base-package="com.liu.pojo"/>

Test class

public class StudentTest {
    
    
    @Test
    public void test(){
    
    
        ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student myStudent =(Student) classPathXmlApplicationContext.getBean("student");
        System.out.println(myStudent);
    }
}

Test one (@Autowired default type)

The annotation method introduces Student into spring
and adds @Autowired annotation to the school attribute of Student. The
annotation method introduces PrimarySchool into spring

@Component
public class Student {
    
    
    @Autowired
    private School school;
    @Value("张三")
    private String sname;
    @Value("22")
    private int  sage;
   //。。。省略其他代码
}
@Component("myShool")
public class PrimarySchool extends School {
    
    
}

After the test class runs, the result 1 is
Insert picture description here
then delete the @Component annotation of PrimarySchool, and add the @Component annotation to the School class

public class PrimarySchool extends School {
    
    
}
@Component("myShool")
public class School {
    
    
    @Value("北京大学附属一中")
   private String name;
    @Value("私立小学")
   private String type;
   //。。。省略其他代码
}

Result after the test class is run 2
Insert picture description here

Test one conclusion

According to the results 1 and 2 after running, it can be seen that the school attribute of Student can be added @Autowired to introduce the School type or the subclass PrimarySchool of School, and the specified value of @Component("myShool") is not "shool", thus It is concluded that **@Autowired belongs to the bytype type by default, @Autowired can automatically introduce objects of the same type by default (that is, objects of a certain type or subclass objects of this type can be introduced), and the id of the been in spring (that is, "MyShool") specified by @Component("myShool") does not matter **

Test two (@Autowired type can also set the byName introduction method)

@Qualifier("school") can introduce @Autowired into the way set byName
@Autowired(required = false)required The default is true. If school is null, cuo will be reported, and required = false will not report an error

@Component
public class Student {
    
    
    @Autowired(required = false)
    @Qualifier("school")
    private School school;
    @Value("张三")
    private String sname;
    @Value("22")
    private int  sage;
    //。。。省略其他代码
}

After the test class is run, the result 3
Insert picture description here
sets the value specified by Qualifier to the id of the spring specified object (that is, the value specified by the myShool class @Component("myShool"))

@Component
public class Student {
    
    
    @Autowired(required = false)
    @Qualifier("myShool")
    private School school;
    @Value("张三")
    private String sname;
    @Value("22")
    private int  sage;
    //。。。省略其他代码
}

Result after running the test class 4
Insert picture description here

Test two conclusion

@Qualifier annotation can set @Autowired introduction method to byName

Test three (@Resource is first introduced according to byName, if byName is not found, it will be imported according to byType)

PrimarySchool class @Component specified value is "myShool"
School of Student class is introduced by @Resource

@Component("myShool")
public class PrimarySchool extends School {
    
    
}
public class School {
    
    
    @Value("北京大学附属一中")
   private String name;
    @Value("私立小学")
   private String type;
  //。。。省略其他代码
}
@Component
public class Student {
    
    
    @Resource
    private School school;
    @Value("张三")
    private String sname;
    @Value("22")
    private int  sage;
    //。。。省略其他代码
}

The result after the test class is run 5
Insert picture description here

Set the @Resource of Student to only use the byName method @Resource(name = "school")

@Component
public class Student {
    
    

    @Resource(name = "school")
    private School school;
    @Value("张三")
    private String sname;
    @Value("22")
    private int  sage;
    //。。。省略其他代码 
}

The result after the test class is run 7
Insert picture description here

Test three conclusion

@Resource is first introduced according to byName by default. If byName is not found, it will be imported according to byType. If @Resource sets the byName method, if the id of been cannot be found, an error will be reported.

Guess you like

Origin blog.csdn.net/weixin_45742032/article/details/110680103