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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值