Spring Bean的自动装配

Posted amcomputer

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Bean的自动装配相关的知识,希望对你有一定的参考价值。

一个人有2个宠物,分别为cat 和dog
在这里插入图片描述

package com.yang;

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



package com.yang;

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



package com.yang;

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 + '\\'' +
                '}';
    }
}



IOC容器内容为:


<?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:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       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
">
<!--    开启自动注解支持-->
<context:annotation-config></context:annotation-config>

     <bean id="cat" class="com.yang.Cat"/>
    <bean id="dog" class="com.yang.Dog"/>


    <bean id="people" class="com.yang.People" autowire="byName">
        <!-- collaborators and configuration for this bean go here -->
        <property name="name" value="yyl"> </property>
<!--        <property name="dog" value="yyl"> </property>-->
<!--        <property name="cat" value="yyl"> </property>-->
    </bean>

</beans>

测试类

import com.yang.People;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Mytest {
    public static void main(String[] args) {
        //获取上下文
        ApplicationContext conteWxt = new ClassPathXmlApplicationContext("auto.xml");
        //拿去对
        //Student stu = (Student)conteWxt.getBean("student");
        People people = (People)conteWxt.getBean("people",People.class);
        people.getCat().shout();
        people.getDog().shout();
    }
}

运行测试类结果:
在这里插入图片描述
总结:
在这里插入图片描述

以上是关于Spring Bean的自动装配的主要内容,如果未能解决你的问题,请参考以下文章

Spring5 Java代码装配Bean

Spring学习笔记之装配Bean

spring:按照Bean的名称自动装配User

spring 自动装配 bean 有哪些方式?

Spring 实战-第二章-装配Bean

Spring基础:Bean自动装配