spring学习10:Bean的自动装配

spring学习10:Bean的自动装配

  • 自动装配:

    • 自动装配是Spring满足Bean依赖的一种方式;

    • Spring会在上下文中自动寻找,并自动给bean装配属性;

 

  • Spring有3种装配方式:

    • 在XML中显示的配置;

    • 在Java中显示配置;

    • 隐式的自动装配bean【重要】;

 

 

  • 环境搭建:XML显式配置

    • 一个人有2个宠物;

    • 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
             http://www.springframework.org/schema/beans/spring-beans.xsd">

         <bean id="cat" class="com.ljxdemo.pojo.Cat"/>

         <bean id="dog" class="com.ljxdemo.pojo.Dog"/>

         <bean id="person" class="com.ljxdemo.pojo.Person">
             <property name="name" value="张三"/>
             <property name="cat" ref="cat"/>
             <property name="dog" ref="dog"/>
          </bean>


      </beans>
    • pojo类:Person ,Cat ,Dog

      public class Person {

         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 "Person{" +
                     "cat=" + cat +
                     ", dog=" + dog +
                     ", name='" + name + '\'' +
                     '}';
        }
      }
    • 测试类:

      public class MyTest {
         @Test
         public void test(){
             ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
             Person person = (Person)context.getBean("person");
             person.getDog().shout();
             person.getCat().shout();
             System.out.println(person);
        }
      }

       

 

  • 隐式的自动装配bean【重要】:

    • ByName自动装配;

    • ByType自动装配;

 

 

  • ByName自动装配:autowire="byName"

    • byName:会自动在容器上下文中查找,和自己对象set方法后面的值对应的beanId;

    • xml配置文件:autowire="byName"

      <?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
             http://www.springframework.org/schema/beans/spring-beans.xsd">

         <bean id="cat" class="com.ljxdemo.pojo.Cat"/>

         <bean id="dog" class="com.ljxdemo.pojo.Dog"/>

         <!--
             byName:会自动在容器上下文中查找,和自己对象set方法后面的值对应的beanId
         -->
         <bean id="person" class="com.ljxdemo.pojo.Person" autowire="byName">
             <property name="name" value="张三"/>
         </bean>


      </beans>

 

 

  • ByType自动装配:autowire="byType"

    • byType:会自动在容器上下文中查找,和自己对象属性类型相同的beanId;

    • 用byType时需注意:

      • 类型必须全局唯一

      • 对应的bean中,id可以省略

      <?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
             http://www.springframework.org/schema/beans/spring-beans.xsd">

         <bean id="cat" class="com.ljxdemo.pojo.Cat"/>

         <bean id="dog111" class="com.ljxdemo.pojo.Dog"/>

         <!--
             byType:会自动在容器上下文中查找,和自己对象属性类型相同的beanId
             用byType时需注意:
                 1,类型必须全局唯一;
                 2,对应的bean中,id可以省略
         -->
         <bean id="person" class="com.ljxdemo.pojo.Person" autowire="byType">
             <property name="name" value="张三"/>
         </bean>


      </beans>

       

 

 

  • 总结:

    • byName方式,需要保证所有bean的id唯一,并且这个bean需要和自动注入的属性的set方法的值一致;

    • byType方式,需要保证所有的bean的class唯一,并且这个bean需要和自动注入的属性的类型一致;

 

 

 

 

 

posted @   gzs1024  阅读(100)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示