笔记:Spring基于注解方式实现属性注入,详解@Autowired、@Qulifier、@Resource、@Value四个注解

定义 Student、Teacher 两个类用于测试

  • Student
public class Student {
    private String name;
    private Integer age;

    public Student() {
    }

    public Student(Integer age, String name) {
        this.age = age;
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

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

    public String getName() {
        return name;
    }

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

    public void printStu() {
        System.out.println("this is Student's method!");
    }
}
  • Teacher
public class Teacher {

    private Student student;

    public void print() {
        System.out.println(student.getName() + " " + student.getAge());
        student.printStu();
    }
}



一、 @Autowired

  • XML配置文件为:
    <bean id="teacher" class="spring5.Annotation.Teacher">

    </bean>

    <bean id="student" class="spring5.Annotation.Student">
        <property name="name" value="nash"></property>
        <property name="age" value="18"></property>
    </bean>

1.1 @Autowired的自动装配方式

  • AutowiredbyType的方式进行匹配,在Ioc容器中查找Bean,当有且仅有一个Bean时,spring将进行属性注入,否则抛出异常
  • Autowired不是通过set函数进行装配,故Teacher类中不需要setter方法
	@Autowired
    private Student student;
  • Autowired注解不仅可以用在字段上,也可以用在构造器、setter方法
    如用在构造器上为:
@Autowired
public Teacher(Student student) {
        this.student = student;
}

1.2 运行结果

在这里插入图片描述



二、@Qulifier

2.1 @Qulifier 的使用方法

  • @Qulifier 配合 @Autowired 在有多个相同类型Bean的时候使用
    如xml文件中配置了两个StudentBean:
    <bean id="student1" class="spring5.Annotation.Student">
        <property name="name" value="nash"></property>
        <property name="age" value="18"></property>
    </bean>

    <bean id="student2" class="spring5.Annotation.Student">
        <property name="name" value="dana"></property>
        <property name="age" value="19"></property>
    </bean>

则此时需要使用@Autowired自动装配的话,则要配合@Qulifier

    @Autowired
    @Qualifier("student2")
    private Student student;

2.2 运行结果

在这里插入图片描述



三、@Resource

3.1 @Resource的自动装配方式

  • 如果@Resource后面没有任何内容,则spring默认按byName的方式进行匹配。若按byName找不到,则再按byType匹配,同样,此时若有多个同类型bean,会抛出异常
    <bean id="student" class="spring5.Annotation.Student">
        <property name="name" value="nash"></property>
        <property name="age" value="18"></property>
    </bean>
	@Resource
    private Student student;

运行结果:
在这里插入图片描述

  • 如果@Resource后面指定了nametype,则按指定的name或type去匹配
<bean id="stu" class="spring5.Annotation.Student">
        <property name="name" value="nash"></property>
        <property name="age" value="18"></property>
    </bean>
	@Resource(name = "stu")
    private Student student;

运行结果:
在这里插入图片描述
如果指定的是type,类似:

@Resource(type = Student.class)
    private Student student;

3.2 @Autowired 和 @Resource 的区别

  • @Autowired 默认按byType,而 @Resource 默认按 byName匹配
  • @AutowiredSpring的注解,而 @ResourceJ2EE



四、@Value

  • @Value 不常用,一般用于注入普通类型(如 String、int等)
  • @Value 一般有三种数值填充方式

4.1 @Value(" ")

  • 直接注入普通类型
@Value("nash")
private String name;

@Value("1")
private int age;

@Value("https://www.csdn.net")
private Resource path;

4.2 @Value(#{ })

  • 表示SpEl表达式通常用来获取bean的属性,或者调用bean的某个方法,也可以表示常量
  • 当bean通过@Value(“#{}”) 获取其他bean的属性,或者调用其他bean的方法时,只要该bean (Beab_A)能够访问到被调用的bean(Beab_B),即要么Beab_A 和Beab_B在同一个容器中,或者Beab_B所在容器是Beab_A所在容器的父容器。
@Value("#{T(java.lang.Math).random() * 100}")
private Integer age;
 
@Value("#{systemProperties['os.name']}")
private String osName;
 
@Value("#{1}")
private int number; //获取数字 1

SpEl表达式 详见

4.3 @Value(${ })

  • 一般用于获取外部配置文件的属性值
    config.properties
name = nash
@Value("${name}")
private String name;

参考

Spring @Value 注解赋值

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值