【框架:Spring】万字详解IOC控制反转(下:注解)

2022年05月15日 阅读数:5
这篇文章主要向大家介绍【框架:Spring】万字详解IOC控制反转(下:注解),主要内容包括基础应用、实用技巧、原理机制等方面,希望对大家有所帮助。

1.使用注解的步骤

在这里插入图片描述

2.定义Bean的注解@Component

2.1 @Component

在这里插入图片描述
Student.javajava

package com.sdnu.ba01;

import org.springframework.stereotype.Component;

/**
 * @Component("myStudent")等同于
 * <bean id="myStudent" class="com.sdnu.ba01.Student"/>
 */
@Component("myStudent")
public class Student {
   
   
    private String name;
    private Integer age;

    public String getName() {
   
   
        return name;
    }

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

    public Integer getAge() {
   
   
        return age;
    }

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

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

在这里插入图片描述
applicationContext.xmlgit

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

    <context:component-scan base-package="com.sdnu.ba01"/>
</beans>

测试代码github

    @Test
    public void test1(){
   
   
        String config = "applicationContext.xml";
        ApplicationContext ctx = new ClassPathXmlApplicationContext(config);
        Student student = (Student)ctx.getBean("myStudent");
        System.out.println(student);
    }

2.2 @Repository @Service @Controller

在这里插入图片描述

2.3 指定多个包的方式

2.3.1 使用屡次扫描器,指定不一样的包

<context:component-scan base-package="com.sdnu.ba01"/>
<context:component-scan base-package="com.sdnu.ba02"/>

2.3.2 使用分隔符(;或者,)分割多个包名

<context:component-scan base-package="com.sdnu.ba01; com.sdnu.ba02"/>

2.3.3 指定父包

<context:component-scan base-package="com.sdnu"/>

3.简单类型属性注入@Value

在这里插入图片描述
Student.javaweb

package com.sdnu.ba01;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @Component("myStudent")等同于
 * <bean id="myStudent" class="com.sdnu.ba01.Student"/>
 */
@Component("myStudent")
public class Student {
   
   
    @Value("冰冰")
    private String name;
    private Integer age;

    public String getName() {
   
   
        return name;
    }

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

    public Integer getAge() {
   
   
        return age;
    }
    @Value("21")
    public void setAge(Integer age) {
   
   
        this.age = age;
    }

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

4.byType自动注入 @Autowired

在这里插入图片描述

School.javaspring

package com.sdnu.ba02;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("mySchool")
public class School {
   
   
    @Value("深圳大学")
    private String name;
    @Value("深圳市")
    private String address;

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

    public void setAddress(String address) {
   
   
        this.address = address;
    }

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

Student.javaapp

package com.sdnu.ba02;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @Component("myStudent")等同于
 * <bean id="myStudent" class="com.sdnu.ba01.Student"/>
 */
@Component("myStudent2")
public class Student {
   
   
    @Value("冰冰")
    private String name;
    @Value("21")
    private Integer age;
    @Autowired
    private School school;

    public School getSchool() {
   
   
        return school;
    }

    public void setSchool(School school) {
   
   
        this.school = school;
    }

    public String getName() {
   
   
        return name;
    }

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

    public Integer getAge() {
   
   
        return age;
    }

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

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

5. byName自动注入 @Autowired 和 @ Qualifier

在这里插入图片描述

School.javaide

package com.sdnu.ba03;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("mySchool")
public class School {
   
   
    @Value("深圳大学")
    private String name;
    @Value("深圳市")
    private String address;

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

    public void setAddress(String address) {
   
   
        this.address = address;
    }

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

Student.javasvg

package com.sdnu.ba03;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("myStudent3")
public class Student {
   
   
    @Value("冰冰")
    private String name;
    @Value("21")
    private Integer age;
    @Autowired
    @Qualifier("mySchool")
    private School school;

    public void setSchool(School school) {
   
   
        this.school = school;
    }

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

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

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

6.JDK注解 @Resource自动注入

在这里插入图片描述
Student.java测试

package com.sdnu.ba04;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component("myStudent4")
public class Student {
   
   
    @Value("冰冰")
    private String name;
    @Value("21")
    private Integer age;
    @Resource
    private School school;

    public void setSchool(School school) {
   
   
        this.school = school;
    }

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

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

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

在这里插入图片描述

7.注解与xml的对比

Student.javathis

package com.sdnu.ba04;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component("myStudent4")
public class Student {
   
   
    @Value("${myName}")
    private String name;
    @Value("${myAge}")
    private Integer age;
    @Resource
    private School school;

    public void setSchool(School school) {
   
   
        this.school = school;
    }

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

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

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

在这里插入图片描述

做者:Beyong    
出处:Beyong博客
github地址:https://github.com/beyong2019

本博客中未标明转载的文章归做者Beyong有,欢迎转载,但未经做者赞成必须保留此段声明,且在文章明显位置给出原文链接,不然保留追究法律责任的权利。