Spring注解@Autowired

Spring注解@Autowired

一、@Autowired注解作用

1、@Autowired

注解@Autowired是Spring对组件自动装配的一种方式。常用于在一个组件中引入其他组件。

//引入Person组件
@Autowired
Person person;

自动装配:sprng通过依赖注入(DI),完成IOC容器中各个组件依赖的关系赋值

2、@Autowired自动装配详解
(1) @Autowired自动装配,默认优先按照类型取IOC容器中寻找对应的组件
annotationConfigApplicationContext.getBean(Person.class);
(2) 如果有多个相同类型的组件,再将属性的名称作为组件的id去容器中查找
annotationConfigApplicationContext.getBean("person");
(3) @Autowired配合@Primary使用,当使用@Autowired自动装配时,默认优先选择被注解@Primary标注的组件
@Primary
@Bean
public Person person(){
    return new Person("张三");
}
(4) @Autowired配合@Qualifier使用,使用注解@Qualifier可以指定需要装配组件的id
@Qualifier("person")
@Autowired
Person person;

(5) 使用@Autowired自动装配,如果在容器中找不到对应的组件,则会报错
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.dashu.bean.Person' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

(6) @Autowired属性required,默认为true,如果在容器中找不到对应的组件,则会报错;如果为false,如果在容器中找不到对应的组件,则不装配,不会报错;
@Autowired(required = false)
Person person;
@Target({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Autowired {

   boolean required() default true;

}

二、@Autowired自动装配使用案例

1、项目结构

Spring注解@Autowired_第1张图片

2、Person
public class Person {

    private String name;

    public Person() {}

    public Person(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                '}';
    }
}
3、Bean注入配置类
import com.dashu.bean.Person;
import org.springframework.context.annotation.*;

@ComponentScan({"com.dashu"})
@Configuration
public class BeanConfig {

    @Bean
    public Person person(){
        return new Person("张三");
    }

}
4、TestService
@Service
public class TestService {

    @Autowired
    Person person;


    public void test(){
        System.out.println(person);
    }

}
5、测试类
import com.dashu.config.BeanConfig;
import com.dashu.service.TestService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;


public class Main {

    public static void main(String[] args) {

        //加载配置类获取容器
        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(BeanConfig.class);

        //容器中获取TestService
        TestService testService = annotationConfigApplicationContext.getBean(TestService.class);

        //调用测试方法
        testService.test();


    }

}
6、运行结果

Spring注解@Autowired_第2张图片

三、多个相同类型的组件

如果有多个相同类型的组件,再将属性的名称作为组件的id去容器中查找

annotationConfigApplicationContext.getBean("person");
1、Bean注册配置类
import com.dashu.bean.Person;
import org.springframework.context.annotation.*;

@ComponentScan({"com.dashu"})
@Configuration
public class BeanConfig {

    @Bean
    public Person person1(){
        return new Person("张三");
    }

    @Bean
    public Person person2(){
        return new Person("李四");
    }

}
2、TestService
import com.dashu.bean.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TestService {


    @Autowired
    Person person2;


    public void test(){
        System.out.println(person2);
    }

}
3、运行结果

Spring注解@Autowired_第3张图片

四、@Autowired配合@Primary使用

@Autowired配合@Primary使用,当使用@Autowired自动装配时,默认优先选择被注解@Primary标注的组件

@Primary
@Bean
public Person person(){
    return new Person("张三");
}
1、Bean注册配置类
import com.dashu.bean.Person;
import org.springframework.context.annotation.*;

@ComponentScan({"com.dashu"})
@Configuration
public class BeanConfig {

    @Bean
    public Person person1(){
        return new Person("张三");
    }

    @Primary
    @Bean
    public Person person2(){
        return new Person("李四");
    }

}
2、TestService
import com.dashu.bean.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TestService {


    @Autowired
    Person person;


    public void test(){
        System.out.println(person);
    }

}
3、运行结果

Spring注解@Autowired_第4张图片

五、@Autowired配合@Qualifier使用

@Autowired配合@Qualifier使用,使用注解@Qualifier可以指定需要装配组件的id

@Qualifier("person")
@Autowired
Person person;

1、Bean注册配置类
import com.dashu.bean.Person;
import org.springframework.context.annotation.*;

@ComponentScan({"com.dashu"})
@Configuration
public class BeanConfig {

    @Bean("person1")
    public Person person1(){
        return new Person("张三");
    }


    @Bean("person2")
    public Person person2(){
        return new Person("李四");
    }

}
2、TestService
import com.dashu.bean.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TestService {


    @Qualifier("person2")
    @Autowired
    Person person;


    public void test(){
        System.out.println(person);
    }

}
3、运行结果

Spring注解@Autowired_第5张图片

你可能感兴趣的:(Spring,spring,Autowired,Primary,Qualifier,自动装配)