自动装配Bean

Bean的自动装配

  • 自动装配是Spring满足bean依赖的一种方式!
  • Spring会在上下文中自动寻找,并自动给bean装配属性
    在Spring中有三种装配的方式
  1. 在xml中显式的配置
  2. 在java中显式配置
  3. 隐式的自动装配bean【重要】

搭建环境:一个人有两个宠物

people

package com.wx.pojo;

public class People {
    private Cat cat;
    private Dog dog;
    private String name;

    public Cat getCat() {
        return cat;
    }

    public void setCat(Cat cat) {
        this.cat = cat;
    }

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "People{" +
                "cat=" + cat +
                ", dog=" + dog +
                ", name='" + name + '\'' +
                '}';
    }
}

cat

package com.wx.pojo;

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

dog

package com.wx.pojo;

public class Dog {
    public void shout(){
        System.out.println("wang~");
    }
}

ByName自动装配

我们在bean标签中添加 autowire="byName"属性,然后就可以通过自己对象set后面的值对象我们bean的id自动装配

<?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="cat" class="com.wx.pojo.Cat"/>
<bean id="dog" class="com.wx.pojo.Dog"/>
<bean id="people" class="com.wx.pojo.People" autowire="byName">
    <property name="name" value="wx"/>
</bean>
</beans>

测试

import com.wx.pojo.People;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    @Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        People people = context.getBean("people", People.class);
        people.getDog().shout();
        people.getCat().shout();
    }
}

在这里插入图片描述

ByType自动装配

我们在bean标签中添加 autowire="byType"属性,会自动在容器上下文查找,和自己对象属性类型相同的bean

<?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="cat" class="com.wx.pojo.Cat"/>
<bean id="dog" class="com.wx.pojo.Dog"/>
<bean id="people" class="com.wx.pojo.People" autowire="byType">
    <property name="name" value="wx"/>
</bean>
</beans>

测试

import com.wx.pojo.People;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    @Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        People people = context.getBean("people", People.class);
        people.getDog().shout();
        people.getCat().shout();
    }
}

在这里插入图片描述
小结

  • 使用ByName的时候,需要保证所有bean的id唯一,并且bean需要和自动配置的属性的set方法的值一致
  • 使用ByType的时候,需要保证bean的class唯一,并且这个bean需要和自动注入的属性类型一致

实现注解实现自动装配

jdk1.5支持的注解,Spring2.5支持注解
要使用注解需知:

  1. 导入约束xmlns:context="http://www.springframework.org/schema/context" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  2. 配置注解的支持<context:annotation-config/>
<?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
http://www.springframework.org/schema/context/spring-context.xsd">
    <context:annotation-config/>
<bean id="cat" class="com.wx.pojo.Cat"/>
<bean id="dog" class="com.wx.pojo.Dog"/>
<bean id="people" class="com.wx.pojo.People"/>
</beans>

@Autowired

直接在属性上使用即可!也可以在set方式上使用!
@Autowired(required=false) 说明: false,对象可以为null;true,对象必须存对象,不能为null。

package com.wx.pojo;

import org.springframework.beans.factory.annotation.Autowired;


public class People {
    @Autowired
    private Cat cat;
    @Autowired
    private Dog dog;
    private String name;

    public Cat getCat() {
        return cat;
    }

    public void setCat(Cat cat) {
        this.cat = cat;
    }

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "People{" +
                "cat=" + cat +
                ", dog=" + dog +
                ", name='" + name + '\'' +
                '}';
    }
}

Qualifier

  • @Autowired是根据ByType自动装配的,加上@Qualifier则可以根据ByName的方式自动装配
  • @Qualifier不能单独使用。
    测试
    添加几个id不一样类一样的对象
<bean id="cat" class="com.wx.pojo.Cat"/>
<bean id="cat1" class="com.wx.pojo.Cat"/>
<bean id="dog" class="com.wx.pojo.Dog"/>
<bean id="dog1" class="com.wx.pojo.Dog"/>
<bean id="people" class="com.wx.pojo.People"/>

没有加入Qualifier注解运行直接报错
加入Qualifier注解

public class People {
    @Autowired
    @Qualifier(value = "cat")
    private Cat cat;
    @Autowired
    @Qualifier(value = "dog")
    private Dog dog;
    private String name;

    public Cat getCat() {
        return cat;
    }

    public void setCat(Cat cat) {
        this.cat = cat;
    }

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "People{" +
                "cat=" + cat +
                ", dog=" + dog +
                ", name='" + name + '\'' +
                '}';
    }
}

测试
在这里插入图片描述

Resource

这个注解在不是spring的,是jdk的,在高版本的jdk已经取消了

  • @Resource如有指定的name属性,先按该属性进行byName方式查找装配;
  • 其次再进行默认的byName方式进行装配;
  • 如果以上都不成功,则按byType的方式自动装配。
  • 都不成功,则报异常。
public class People {
    @Resource(name = "cat")
    private Cat cat;
    @Autowired
    @Qualifier(value = "dog")
    private Dog dog;
    private String name;

小结:
@Resource与 @Autowired区别:

  • 都是用来自动装配的,都可以放在属性字段上
  • @Autowired通过ByType的方式实现
  • @Resource如有指定的name属性,先按该属性进行byName方式查找装配,其次再进行默认的byName方式进行装配 如果以上都不成功,则按byType的方式自动装配。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值