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、项目结构

在这里插入图片描述

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、运行结果

在这里插入图片描述

三、多个相同类型的组件

如果有多个相同类型的组件,再将属性的名称作为组件的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、运行结果

在这里插入图片描述

四、@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、运行结果

在这里插入图片描述

五、@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、运行结果

在这里插入图片描述

  • 14
    点赞
  • 67
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大树下躲雨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值