Bean的自动装配01

自动装配是Spring满足Bean依赖的一种方式;Spring会在上下文自动寻找,并自动给bean装配属性。

在Spring中有三种装配的方式:

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

1、搭建环境

  1. 编写实体类;

    package com.beyond.pojo;
    
    public class Cat {
        public void shut(){
            System.out.println("喵~");
        }
    }
    
    package com.beyond.pojo;
    
    public class Dog {
        public void shut(){
            System.out.println("汪~");
        }
    }
    
    package com.beyond.pojo;
    
    public class People {
        private String name;
        private Cat cat;
        private Dog dog;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = 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;
        }
    
        @Override
        public String toString() {
            return "People{" +
                    "name='" + name + '\'' +
                    ", cat=" + cat +
                    ", dog=" + dog +
                    '}';
        }
    }
    
  2. 编写beans.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="cat" class="com.beyond.pojo.Cat"/>
        <bean id="dog" class="com.beyond.pojo.Dog"/>
    
        <bean id="people" class="com.beyond.pojo.People">
            <property name="name" value="小明"/>
            <property name="cat" ref="cat"/>
            <property name="dog" ref="dog"/>
        </bean>
    
    </beans>
    
  3. 测试。

    import com.beyond.pojo.People;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class MyTest {
        @Test
        public void test01(){
            ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
    
            People people = context.getBean("people", People.class);
            people.getCat().shut();
            people.getDog().shut();
        }
    }
    

2、byName自动装配

  1. 编写beans.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="cat" class="com.beyond.pojo.Cat"/>
        <bean id="dogccwd" class="com.beyond.pojo.Dog"/>
    
        <!--
        autowire="byName"表示会自动在容器上下文中查找,和自己对象set方法后面的值对应的 beanid
        -->
        <bean id="people" class="com.beyond.pojo.People" autowire="byName">
            <property name="name" value="小明"/>
        </bean>
    
    </beans>
    
  2. 测试。

    import com.beyond.pojo.People;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class MyTest {
        @Test
        public void test01(){
            ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
    
            People people = context.getBean("people", People.class);
    
            /*
            喵~
    
            java.lang.NullPointerException              空指针异常
                at MyTest.test01(MyTest.java:13)
             */
            people.getCat().shut();
            people.getDog().shut();
        }
    }
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值