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();
    }
}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值