Spring(7) 配置合作者Bean

  1. 一个类中的成员变量如果是类所处下spring的其他bean,可以在配置文件中配置,这种操作叫做配置合作者bean
  2. 显示地配置合作者bean应当在property标签下使用标签来指定合作者bean,操作如下
  3. 程序结构
  4. public class ExampleBean {
       
        private Test test;
    //    ExampleBean类拥有Test类型的变量,并且还需要有test变量的set方法
        public Test getTest() {
            return test;
        }
    
        public void setTest(Test test) {
            this.test = test;
        }
    
     
    }
    
    public class Test {
        private String tea;
    
        public String getTea() {
            return tea;
        }
    
        public void setTea(String tea) {
            this.tea = tea;
        }
    }
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    /**
     * 测试类,测试有没有被赋值成功
     */
    public class SpringTest {
        public static void main(String []args){
            ApplicationContext applicationContext =
                    new ClassPathXmlApplicationContext("beans.xml");
            ExampleBean exampleBean = (ExampleBean)applicationContext.getBean("exampleField",ExampleBean.class);
            Test test = (Test)applicationContext.getBean("test", Test.class);
            System.out.println(test.getTea());
            System.out.println(exampleBean.getTest().getTea());
        }
    }
    
    <?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="test" class="Test">
            <property name="tea" value="redTea"/>
        </bean>
    
        <bean id="exampleField" class="ExampleBean" >
           <property name="test" ref="test"/>
          <!--显示指定合作者bean-->
        </bean>
    
    </beans>

     

  5. 使用自动装配注入合作者bean,spring可以自动装配bean和bean之间的一来关系,从而无需使用ref显示指定,而是由spring检查xml配置文件的内容,根据以下两种规则,完成注入

    1. byname:如果在bean A里面有setB()方法,而spring配置文件下刚好有一个id为b的bean,那么spring会把b的实例注入到bean A里边

      import org.springframework.context.ApplicationContext;
      import org.springframework.context.support.ClassPathXmlApplicationContext;
      
      /**
       * 测试类,测试有没有被赋值成功
       */
      public class SpringTest {
          public static void main(String []args){
              ApplicationContext applicationContext =
                      new ClassPathXmlApplicationContext("beans.xml");
              ExampleBean exampleBean = (ExampleBean)applicationContext.getBean("exampleField",ExampleBean.class);
              System.out.println(exampleBean.getByNameTest().getTea());
          }
      }
      
      <?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="byNameTest" class="Test">
              <!--spring里边有一个id为byNameTest的bean,它就会被注入到exampleField里边-->
              <property name="tea" value="redTea"/>
          </bean>
      
          <bean id="exampleField" class="ExampleBean"  autowire="byName">
          </bean>
      
      </beans>
      public class ExampleBean {
      
          private Test byNameTest;
      //    ExampleBean类拥有byNameTest类型的变量,并且还需要有test变量的set方法
      
      
          public Test getByNameTest() {
              return byNameTest;
          }
      
          public void setByNameTest(Test byNameTest) {
              this.byNameTest = byNameTest;
          }
      }
      

       

    2. byType方法,如果bena A有方法setB(B b),而xml配置文件里边刚好有个id为b的bean,那么spring会把bean a注入到bean a里边

      public class ExampleBean {
          
      
          private Test test;
      
          public Test getTest() {
              return test;
          }
      //bean ExampleBean有setB(B b)方法,xml配置文件按有id为b的bean
          public void setTest(Test test) {
              this.test = test;
          }
      
      }
      
      <?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="test" class="Test">
              <!--spring里边有一个id为test的bean,它就会被注入到exampleField里边-->
              <property name="tea" value="redTea"/>
          </bean>
      
          <bean id="exampleField" class="ExampleBean"  autowire="byType"/>
      
      
      </beans>

      这是我看李刚编著的《轻量级javaEE企业应用实战(第五版)-Struts2+Spring5+Hibernate5/JAP2》后总结出来的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值