Bean的自动装配

Bean的自动装配

  1. 实现一人两宠物

    1. cat

      public class Cat {
      
          public void Cry(){
              System.out.println("哈气");
          }
      
      }
      
    2. dog

      public class Dog {
          public void Cry(){
              System.out.println("狗叫");
          }
      
      }
      
    3. people

      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 +
                      '}';
          }
      }
      
    4. beans.xml

      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"
             xmlns:c="http://www.springframework.org/schema/c"
             xmlns:p="http://www.springframework.org/schema/p"
             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.wjj.pojo.Cat">
              </bean>
              <bean id="dog" class="com.wjj.pojo.Dog">
              </bean>
              <bean id="people" class="com.wjj.pojo.People">
                  <property name="name" value="577">
                  </property>
                  <property name="cat" ref="cat">
                  </property>
                  <property name="dog" ref="dog">
                  </property>
              </bean>
      </beans>
      
    5. 测试

      import org.springframework.context.ApplicationContext;
      import org.springframework.context.support.ClassPathXmlApplicationContext;
      
      public class Test {
          @org.junit.Test
          public void test(){
           ApplicationContext Context = new ClassPathXmlApplicationContext("beans.xml");
              People people = Context.getBean(People.class);
              people.getCat().Cry();
              people.getDog().Cry();
      
          }
      }
      

      image-20260317180656790

  2. Byname自动装配与byType自动装配

       <!-- byName自动装配   会自动查找上下文与自己set方法后面名字相同的值的呀的Bean id        -->
        <bean id="people" class="com.wjj.pojo.People" autowire="byName">
                </bean>
    
    
    <!--bytype 会自动查找上下文与自己属性类型相同的bean-->      
        <bean id="people" class="com.wjj.pojo.People" autowire="byType">
        </bean>
    

    小结

    • byName时候,需要保证所有的bean的id唯一,并且这个bean id需要和自动注入的属性的set方法的值一致
    • byType的时候,需要宝藏bean的class唯一,并且这个bean需要和自动注入的属性类型一致
  3. 使用注解实现自动装配

    1. 导入context的约束

    2. 开启支持注解:context:annotation-config/

    3. @Autowired

      • 直接用在set方法上或者属性上
      • 判断名字或者属性类型是否唯一
      • 通过反射可以不写set方法
      • 可以用@Qualifier (value = "指定名")来指定

      代码

      @Autowired
      @Qualifier (value = "cat1")
      private Cat cat;
      @Autowired
      @Qualifier (value = "dog1")
      private Dog dog;
      
posted @ 2026-03-17 20:37  三咲肘击王  阅读(8)  评论(0)    收藏  举报