Spring基础(三)

Bean的自动装配

  • spring会在上下文中自动寻找,并自动给bean装配属性

  • 自动装配是spring满足bean依赖的一种方式

之前bean的属性从头到尾都是人为手动放进去的,使用自动装配后不需要手动写属性,spring会根据上下文自动的将对象的属性装配进去!

三种装配方式

  • 在xml中显示配置(之前一直使用的)
  • 在java中显示配置
  • 隐式的自动装配重要

隐式自动装配(autowire)

例如:

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

    <bean id="dog" class="Dog"/>
    <bean id="cat" class="Cat"/>
    <bean id="person" class="Person">
        <property name="cat" ref="cat"/>
        <property name="dog" ref="dog"/>
        <property name="name" value="小张"/>
    </bean>

</beans>
public class Dog {
    public void said(){
        System.out.println("wang~");
    }
}

public class Cat {
    public void said(){
        System.out.println("miao~");
    }
}

public class Person {
    private Dog dog;
    private Cat cat;
    private String name;
    public Dog getDog() {
        return dog;
    }
    public void setDog(Dog dog) {
        this.dog = dog;
    }
    public Cat getCat() {
        return cat;
    }
    public void setCat(Cat cat) {
        this.cat = cat;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

ByName自动装配

例如在上面的例子beans中,我们的person对象中有属性名为cat、dog的属性,spring容器中有cat、dog名的对象,我想如果他们能够自动匹配就不需要我再写那两行注入属性的代码了。ByName就提供了这种机制!

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

    <bean id="dog" class="Dog"/>
    <bean id="cat" class="Cat"/>
    <!-- 增加了autowire的属性dog、cat自动注入到对象的属性中了 -->
    <bean id="person" class="Person" autowire="byName">
        <property name="name" value="小张"/>
    </bean>

</beans>

ByType自动装配

例如在上面的例子beans中,我们的person对象中有属性名为cat、dog的属性,spring容器中假设有cat1、dog1名的对象,我想如果他们能够按照类型自动匹配就不需要我再写那两行注入属性的代码了。ByType就提供了这种机制!

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

    <bean id="dog1" class="Dog"/>
    <bean id="cat1" class="Cat"/>
    <bean id="person" class="Person" autowire="byType">
        <property name="name" value="小张"/>
    </bean>

</beans>
  • 使用byName时,需要保证所有bean的id唯一
  • 使用byType时,需要保证所有bean的类型唯一

使用注解进行装配

要使用注解需要:

  • 导入约束

    context约束

  • 配置注解的支持

    context支持

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 注意要使用这个标签进行注解支持;以及上面的url导入约束 -->
    <context:annotation-config/>

    <bean id="dog1" class="Dog"/>
    <bean id="cat1" class="Cat"/>
    <bean id="person" class="Person"/>
</beans>
public class Person {
    @Autowired
    private Dog dog;
    @Autowired
    private Cat cat;
    private String name;
    @Override
    public String toString() {
        return "Person{" +
                "dog=" + dog +
                ", cat=" + cat +
                ", name='" + name + '\'' +
                '}';
    }
    public Dog getDog() {
        return dog;
    }
    public Cat getCat() {
        return cat;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

使用@Autowire注解后,自动匹配xml中的对象注入到属性中(包括名称匹配、类型匹配)。使用注解后可以省略set方法

允许值为空的注解

补充:

  1. 对象可以为null:将注解的属性required设为false
public class Person {
    @Autowired(required = false)
    private Dog dog;
    @Autowired
    private Cat cat;
    private String name;
}
  1. 参数值可以为空:使用 @Nullable
public class Person {
    private String name;
    public String getName() {
        return name;
    }
    // 这样这个参数值就可以传进来空值而不报错了
    public void setName(@Nullable String name) {
        this.name = name;
    }
}

配合Qualifier指定bean

如果配置环境比较复杂,可以使用 @Qualifier(value = “xxx”) 注解来配合使用,value指定的是bean的id名!

public class Person {
    @Autowired
    @Qualifier(value="dog123")
    private Dog dog;
    
    @Autowired
    @Qualifier(value="dog234")
    private Dog dog;
    
    @Autowired
    private Cat cat;
    private String name;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值