spring03:bean的自动装配

spring03:bean的自动装配



前言:

在这里插入图片描述


提示:以下是本篇文章正文内容:

一、 在xml中显示的配置:

分析:

在这里插入图片描述



People类:

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(@Nullable String name) {
        this.name = name;
    }

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

Cat类:

public class Cat {
    public void shut(){
        System.out.println("miaomiaomiao");
    }
}

Dog类:

public class Dog {
    public void shut(){
        System.out.println("wangwangwang");
    }
}

1. 在xml中显示的配置:

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

	// cat 和 dog的 bean对象:
	<bean id="cat" class="com.huxiao.pojo.Cat"></bean>
	<bean id="dog" class="com.huxiao.pojo.Dog"></bean>
	// people的 bean对象:
	<bean id="people" class="com.huxiao.pojo.People">(看这里)
		<property name="name" value="胡潇"></property>
		<property name="cat" ref="cat"></property>
		<property name="dog" ref="dog"></property>
	</bean>
</beans>


二、 隐式的自动装配bean【重要】

分析:

在这里插入图片描述



1. byName:bean id

byname的时候,需要保证所有bean的id唯一,并且这个bean需要和自动注入的属性的set方法的值一致

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

	// cat 和 dog的 bean对象:
	<bean id="cat" class="com.huxiao.pojo.Cat"></bean>
	<bean id="dog" class="com.huxiao.pojo.Dog"></bean>
	// people的 bean对象:
	<bean id="people" class="com.huxiao.pojo.People" autowire="byName">(看这里)
		<property name="name" value="胡潇"></property>
	</bean>
</beans>

2. byType:bean class

bytype的时候,需要保证所有bean的class唯一,并且这个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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

	// cat 和 dog的 bean对象:
	<bean id="cat" class="com.huxiao.pojo.Cat"></bean>
	<bean id="dog" class="com.huxiao.pojo.Dog"></bean>
	// people的 bean对象:
	<bean id="people" class="com.huxiao.pojo.People" autowire="byType">(看这里)
		<property name="name" value="胡潇"></property>
	</bean>
</beans>


三、 使用注解实现自动装配:@Autowired + @Qualifier(value=“”)

分析:

在这里插入图片描述



在这里插入图片描述



在这里插入图片描述


总结

提示:这里对文章进行总结:

💕💕💕

  • 26
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

lennard-lhz

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

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

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

打赏作者

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

抵扣说明:

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

余额充值