Spring5--自动装配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">

    <bean id="cat" class="com.wang.pojo.Cat"/>
<!--    <bean id="dog" class="com.wang.pojo.Dog"/>-->
    <bean id="dog22" class="com.wang.pojo.Dog"/>

<!--    <bean id="people" class="com.wang.pojo.People">-->
<!--        <property name="name" value="小青蛙"/>-->
<!--        <property name="cat" ref="cat"/>-->
<!--        <property name="dog" ref="dog"/>-->
<!--    </bean>-->

    <!--1、byName自动装配   会自动在容器的上下文中查找,和自己对象set方法后面对应的beanid-->
<!--    <bean id="people" class="com.wang.pojo.People" autowire="byName">-->
<!--        <property name="name" value="小青蛙"/>-->
<!--    </bean>-->

    <!--2、byType自动装配   会自动在容器的上下文中查找,和自己对象属性类型相同的beanid
    id可省略  class要唯一
    -->
    <bean id="people" class="com.wang.pojo.People" autowire="byType">
        <property name="name" value="小青蛙"/>
    </bean>

    <!--小结:
    byName的时候,需要保证所有bean的id唯一,并且这个bean需要和自动注入的属性的set方法的值一致。
    byType的时候,需要保证所有的bean的class唯一,并且这个bean需要和自动注入的属性的类型一致。
    -->
</beans>

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

public class MyTest {
    @Test
    public void test1(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

        People people = context.getBean("people", People.class);
        people.getDog().shout();  // wang--
        people.getCat().shout();  // miao--

    }
}

package com.wang.pojo;

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

package com.wang.pojo;

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

package com.wang.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 + '\'' +
                '}';
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringBoot的自动装配原理是通过扫描外部引用jar包中的META-INF/spring.factories文件来加载配置信息,并将配置的类型信息加载到Spring容器中,并执行相应的操作。\[1\]在SpringBoot中,我们只需引入对应的starter依赖,就可以自动完成对第三方依赖的配置操作,无需手动编写特定的bean。\[1\] 具体实现原理是通过在resources目录下创建一个spring.factories文件,文件中配置了需要自动装配的类的全限定名,使用@EnableAutoConfiguration注解来启用自动装配。\[2\]在SpringBoot的主启动类上使用@SpringBootApplication注解,该注解包含了@SpringBootConfiguration、@ComponentScan和@EnableAutoConfiguration三个注解。其中@SpringBootConfiguration注解表示该类是SpringBoot的配置类,@ComponentScan注解用于扫描被@Component注解的bean,@EnableAutoConfiguration注解是自动装配的核心注解。\[3\] 总结起来,SpringBoot的自动装配原理是通过扫描外部引用jar包中的配置文件,加载配置信息,并根据配置信息自动完成对第三方依赖的配置操作,简化了开发者的配置工作。 #### 引用[.reference_title] - *1* *2* *3* [Springboot的自动装配原理](https://blog.csdn.net/weixin_43324905/article/details/123949084)[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^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值