Spring学习笔记[3] -- DI依赖注入

DI 概述

DI:Dependency Injection,依赖注入,在Spring框架负责创建Bean对象的时候,动态的将依赖对象注入到Bean组件中

入门程序

在IOC入门程序中,如果UserServiceImpl的实现类中有一个属性,那么使用Spring框架的IOC功能时,可以通过依赖注把该属性的值传递进来。

创建接口

public interface UserService {
    public void sayHello();
}

编写接口的实现类

public class UserServiceImpl implements UserService {
    private String name;
    public void setName(String name) {
        this.name = name;
    }
    public void sayHello(){
        System.out.println("Demo01: Hello Spring "+name);
    }
}

配置文件

applicationContest.xml中使用bean标签

<bean id="userService" class="gongfukangee.Demo01.UserServiceImpl">
    <!-- 容器在创建 userService 对象的时候,同时给成员属性赋值  -->
    <property name="name" value="Jack"/>
</bean>

创建测试类

    /**
     * @Auther gongfukang
     *依赖注入
     */
    @org.junit.Test
    public void run4(){
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService=(UserService)applicationContext.getBean("userService");
        userService.sayHello();
    }

类成员变量注入

Setter方法注入

  • 成员属性注入

    //实现类
    public class UserServiceImpl implements UserService {
      private String name;
      public void setName(String name) {
          this.name = name;
      }
    }
    <!-- xml配置 -->    
    <bean id="userService" class="gongfukangee.Demo01.UserServiceImpl">
       <property name="name" value="Jack"/>
    </bean>
  • 引用变量注入

    //引用变量实现类
    public class CustomerDaoImpl {
      public void save(){
          System.out.println("我是持久层 DAO ...");
      }
    }
    //实现类
    public class CustomerServiceImpl {
      //提供成员属性
      private CustomerDaoImpl custDao;
      public void setCustDao(CustomerDaoImpl custDao) {
          this.custDao = custDao;
      }
      public void save(){
          System.out.println("我是业务层 Service...");
          custDao.save();
      }
    }
    <!-- xml配置 --> 
    <bean id="customerDao" class="gongfukangee.Demo02.CustomerDaoImpl"/>
    <!-- customerDao 注入到 customerService -->
    <bean id="customerService" class="gongfukangee.Demo02.CustomerServiceImpl">
    <!-- name: 与private 成员属性相同  values :字符串    ref :引用对象 -->
       <property name="custDao" ref="customerDao"/>
    </bean>

构造方法注入

  • 成员属性

    //实现类
    public class Car1 {
      private String carName;
      private double carPrice;
      public Car1(String carName,double carPrice){
          super();
          this.carName=carName;
          this.carPrice=carPrice;
      }
    }
    <!-- xml配置  -->
    <bean id="car1" class="gongfukangee.Demo03.Car1">
    <!-- 第一种注入方式 -->
    <constructor-arg name="carName" value="大众汽车"/>
    <constructor-arg name="carPrice" value="250000.01"/>
    <!-- 第二种注入方式  [index中,0代表构造方法中第一个成员变量] -->
    <constructor-arg index="0" value="奔驰汽车"/>
    <constructor-arg index="1" value="500000"/>
    </bean>
  • 引用变量注入

    //实现类
    public class Person {
      private String PersonName;
      private Car1 car1;
      public Person(String personName, Car1 car1) {
          PersonName = personName;
          this.car1 = car1;
      }
    }
    <!-- xml配置  -->
    <bean id="person" class="gongfukangee.Demo03.Person">
       <constructor-arg name="personName" value="gong"/>
       <constructor-arg name="car1" ref="car1"/>
    </bean>

集合注入和配置文件注入

public class User {
    private String[] arrs;
    public void setArrs(String[] arrs) {
        this.arrs = arrs;
    }

    private List<String> list;
    public void setList(List<String> list) {
        this.list = list;
    }

    private Map<String,String> map;
    public void setMap(Map<String, String> map) {
        this.map = map;
    }

    //配置文件
    private Properties pro;
    public void setPro(Properties pro) {
        this.pro = pro;
    }
}
    <!-- 注入集合 -->
    <bean id="user" class="gongfukangee.Demo03.User">
        <!-- 注入数组 -->
        <property name="arrs">
            <list>
                <value>哈哈</value>
                <value>呵呵</value>
                <value>嘿嘿</value>
            </list>
        </property>

        <!-- 注入List -->
        <!--
         List 存放引用对象
         <property name="list" >
            <list>
               <ref bean=""></ref>
            </list>
        </property>
        -->
        <property name="list" >
            <list>
                <value>小王</value>
                <value>张三</value>
                <value>李四</value>
            </list>
        </property>

        <!-- Map 注入 -->
        <property name="map">
            <map>
                <entry key="AAA" value="刘邦"/>
                <entry key="BBB" value="关羽"/>
            </map>
        </property>

              <!--  Properties 属性文件注入 -->
        <property name="pro">
            <props>
                <prop key="username">root</prop>
                <prop key="password">123456</prop>
            </props>
        </property>

     </bean>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值