08-自动装配Bean

自动装配Bean

Bean的作用域

   User user = context.getBean("user", User.class);
   User user2 = context.getBean("user", User.class);
        System.out.println(user==user2);

Bean的作用域 scope=“singleton” (单例模式)(spring默认机制) (true)

<bean id="user2" class="com.tian.pojo.User" c:age="20" c:name="李四" scope="singleton"/>

原型模式:每次从容器中get的时候,都会产生一个新对象 (false)

scope="prototype"

Bean的自动装配

  • 自动装配是Spring满足bean依赖一种方式!
  • Spring会在上下文中自动寻找,并自动给bean装配属性!

在Spring中有三种装配的方式

  1. 在xml中显示的配置
  2. 在java中显示配置
  3. 隐式的自动装配bean**【重要】**

ByName

<bean id="dog" class="com.tian.pojo.Dog"/>
<bean id="cat" class="com.tian.pojo.Cat"/>   
<!--这样子写cat1会报错-->

<!--  byName:会自动在容器上下文中查找,和自己对象set方法和后面的值对应的bean id  (id名必须为cat和dog)-->
  <bean id="people" class="com.tian.pojo.People" autowire="byName">
       <property name="name" value="张三"/>
   </bean>

ByType

<bean id="dog" class="com.tian.pojo.Dog"/>
<bean id="cat" class="com.tian.pojo.Cat"/>
<!--这样子写cat1不会报错-->

<!-- byType: 会自动在容器上下文中查找,和自己对象属性相同类型的bean (dog111,cat222都行 id还可以省略)  -->
    <bean id="people" class="com.tian.pojo.People" autowire="byType">
        <property name="name" value="张三"/>
    </bean>

环境搭建:一个人有猫和狗

代码演示

实体类

//Dog
public class Dog {
    public void shout(){
        System.out.println("wang~");
    }
}
//Cat
public class Cat {
    public void shout(){
        System.out.println("miao~");
    }
}
//People
package com.tian.pojo;

public class People {
    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;
    }

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

bean.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">


<bean id="dog" class="com.tian.pojo.Dog"/>
<bean id="cat1" class="com.tian.pojo.Cat"/>
<!-- 最原始的方法 -->
<!--<bean id="people" class="com.tian.pojo.People">-->
<!--    <property name="name" value="张三"/>-->
<!--    <property name="cat" ref="cat"/>-->
<!--    <property name="dog" ref="dog"/>-->
<!--</bean>-->

<!--  byName:会自动在容器上下文中查找,和自己对象set方法和后面的值对应的bean id  (id名必须为cat和dog)-->
<!--    <bean id="people" class="com.tian.pojo.People" autowire="byName">-->
<!--        <property name="name" value="张三"/>-->
<!--    </bean>-->

<!-- byType: 会自动在容器上下文中查找,和自己对象属性相同类型的bean (dog111,cat222都行 id可以省略)  -->
    <bean id="people" class="com.tian.pojo.People" autowire="byType">
        <property name="name" value="张三"/>
    </bean>
</beans>

测试

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

小结:

  • byname的时候,需要保证所有bean的id唯一,并且这个bean需要和自动注入的属性的set方法的值一致!.
  • bytype的时候,需要保证所有bean的class唯一,并且这个bean需要和自动注入的属性的类型一致!
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
自动装配bean是指在Spring框架中,通过配置文件或注解的方式,让Spring容器自动将相应的依赖注入到bean中。其中,自动装配bean的方式有多种,其中一种方式是通过byName自动装配。在byName自动装配中,Spring容器会根据bean的属性名称,在容器上下文中查找与属性名称匹配的bean,并将其注入到相应的属性中。\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* [Bean的自动装配](https://blog.csdn.net/qq_41863697/article/details/125135285)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [spring 自动装配 bean 有哪些方式?](https://blog.csdn.net/meism5/article/details/90446694)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Spring | Bean自动装配详解](https://blog.csdn.net/qq_58233406/article/details/127331116)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值