1. 程式人生 > >spring學習-2.spring的兩種注入方式

spring學習-2.spring的兩種注入方式

spring的兩種注入方式

一.設值注入

1.新建一個介面類

public interface Behaviour {
    public String learn();
}

2.實現新建的介面類

  public class Learn implements Behaviour {
      public String learn() {
          return "人會學習";
      }
  }

3.新建一個People物件

 public class People {
      private String name;  //姓名
      private int age;      //年齡
      private Behaviour behaviour;  //行為
  
      public void canDo(){
          System.out.println(behaviour.learn());
      }
  
      //set方法
      public void setBehaviour(Behaviour behaviour) {
          this.behaviour = behaviour;
      }
  
      public String getName() {
          return name;
      }
  
      public int getAge() {
          return age;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public void setAge(int age) {
          this.age = age;
      }
  }

4.配置spring的配置檔案,

 <?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="people" class="edu.njxz.demo.bean.People">
         <property name="name" value="yxc"></property>
         <property name="age" value="21"></property>
         <property name="behaviour" ref="learn"></property>
     </bean>
 
     <bean name="learn" class="edu.njxz.demo.bean.Learn"></bean>
 
 </beans>

5.測試執行

public class Test1 {
    public static void main(String[] args) {

        //把beans.xml的類載入到容器
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("beans.xml");

        //從容器中獲取bean
        People people= (People) applicationContext.getBean("people");
        System.out.println(people.getName()+" "+people.getAge());
        people.canDo();
    }
}

6.執行效果圖
sp_4

二.構造注入

1.還是上面的程式碼,將People類進行簡單的修改

package edu.njxz.demo.bean;

public class People {
    private String name;  //姓名
    private int age;      //年齡
    private Behaviour behaviour;  //行為

    public void canDo(){
        System.out.println(behaviour.learn());
    }


    
    //構造方法
   public People(Behaviour behaviour){
        this.behaviour=behaviour;
   }
    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

2.將配置檔案進行簡單的修改

<?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="people" class="edu.njxz.demo.bean.People">
        <property name="name" value="yxc"></property>
        <property name="age" value="21"></property>
        <!--<property name="behaviour" ref="learn"></property>-->
        <!--使用構造注入,為People例項注入Behavior例項 -->
        <constructor-arg ref="learn"></constructor-arg>
    </bean>

    <bean name="learn" class="edu.njxz.demo.bean.Learn"></bean>

</beans>

3.執行效果圖
sp_4