Spring框架中使用xml配置文件演示依赖注入(DI)

一:依赖注入的两种方式

  • setter注入
  • 构造器注入

二: setter注入引用类型

        1.bean定义引用类型属性并提供可访问的set方法

public class BookServiceImpl implements BookService{
  private BookDao bookDao;  
  public void setBookDao(BookDao bookDao) {
     this.bookDao = bookDao;   
     }
}

        2.配置中使用property标签ref属性注入引用类型对象

<bean id="bookService" class="com.itheima.service.impl.BookServiceImpl">  
  <property name="bookDao" ref="bookDao"/>
</bean>
<bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl"/>

三:setter注入简单类型

         1.bean定义引用类型属性并提供可访问的set方法

public class BookDaoImpl implements BookDao {
    private int connectionNumber;
    public void setConnectionNumber(int connectionNumber) { 
       this.connectionNumber = connectionNumber;
   }
}

         2.配置中使用property标签value属性注入简单类型数据

<bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl">
   <property name="connectionNumber" value="10"/>
</bean>

四:setter注入集合类型

  1.  注入数组类型数据     
    <property name="array">
        <array>
            <value>100</value>
            <value>200</value>
            <value>300</value>
        </array>
    </property>
  2. 注入List类型数据 
    <property name="list">
        <list>
            <value>itcast</value>
            <value>itheima</value>
            <value>boxuegu</value>
            <value>chuanzhihui</value>
        </list>
    </property>
  3. 注入Set类型数据
    <property name="set">
        <set>
            <value>itcast</value>
            <value>itheima</value>
            <value>boxuegu</value>
            <value>boxuegu</value>
        </set>
    </property>
  4. 注入Map类型数据
    <property name="map">
        <map>
            <entry key="country" value="china"/>
            <entry key="province" value="henan"/>
            <entry key="city" value="kaifeng"/>
        </map>
    </property>
     
  5. 注入Propertities类型数据 
    <property name="properties">
        <props>
            <prop key="country">china</prop>
            <prop key="province">henan</prop>
            <prop key="city">kaifeng</prop>
        </props>
    </property>

案例:

public interface BookDao {
    public void save();
}

public class BookDaoImpl implements BookDao {
    private int[] array;
    private List<String> list;
    private Set<String> set;
    private Map<String,String> map;
    private Properties properties;

    public void setArray(int[] array) {
        this.array = array;
    }
    public void setList(List<String> list) {
        this.list = list;
    }
    public void setSet(Set<String> set) {
        this.set = set;
    }
    public void setMap(Map<String, String> map) {
        this.map = map;
    }
    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    public void save() {
        System.out.println("book dao save ...");
        System.out.println("遍历数组:" + Arrays.toString(array));
        System.out.println("遍历List" + list);
        System.out.println("遍历Set" + set);
        System.out.println("遍历Map" + map);
        System.out.println("遍历Properties" + properties);
    }
}
<?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="bookDao" class="com.itheima.dao.impl.BookDaoImpl">
        <!--数组注入-->
        <property name="array">
            <array>
                <value>100</value>
                <value>200</value>
                <value>300</value>
            </array>
        </property>
        <!--list集合注入-->
        <property name="list">
            <list>
                <value>itcast</value>
                <value>itheima</value>
                <value>boxuegu</value>
                <value>chuanzhihui</value>
            </list>
        </property>
        <!--set集合注入-->
        <property name="set">
            <set>
                <value>itcast</value>
                <value>itheima</value>
                <value>boxuegu</value>
                <value>boxuegu</value>
            </set>
        </property>
        <!--map集合注入-->
        <property name="map">
            <map>
                <entry key="country" value="china"/>
                <entry key="province" value="henan"/>
                <entry key="city" value="kaifeng"/>
            </map>
        </property>
        <!--Properties注入-->
        <property name="properties">
            <props>
                <prop key="country">china</prop>
                <prop key="province">henan</prop>
                <prop key="city">kaifeng</prop>
            </props>
        </property>
    </bean>
</beans>
public class AppForDICollection {
    public static void main( String[] args ) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        BookDao bookDao = (BookDao) ctx.getBean("bookDao");
        bookDao.save();
    }
}

五:构造器注入引用类型

        1.bean中定义引用类型属性并提供可访问的构造方法

public class BookServiceImpl implements BookService{
    private BookDao bookDao;
    public BookServiceImpl(BookDao bookDao) {
        this.bookDao = bookDao;
   }
}

        2.配置中使用constructor-arg标签ref属性注入引用类型对象

<bean id="bookService" class="com.itheima.service.impl.BookServiceImpl">
    <constructor-arg name="bookDao" ref="bookDao"/>
</bean>
<bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl"/>

六:构造器注入简单类型

        1.bean定义引用类型属性并提供可访问的set方法

public class BookDaoImpl implements BookDao {
    private int connectionNumber; 
    public void setConnectionNumber(int connectionNumber) {
        this.connectionNumber = connectionNumber;
    }
}

        2.配置中使用constructor-arg标签value属性注入简单类型数据

<bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl"> 
  <constructor-arg name="connectionNumber" value="10"/>
</bean>

Plus:

为了解决根据构造方法参数名称注入耦合的问题还可以按形参类型注入和按形参位置注入

<!--按照形参类型注入-->
<bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl">
    <constructor-arg type="int" value="10"/> 
   <constructor-arg type="java.lang.String" value="mysql"/>
</bean>
<!--按照形参位置注入-->
<bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl">
    <constructor-arg index="0" value="mysql"/>
    <constructor-arg index="1" value="10"/>
</bean>

public class BookDaoImpl implements BookDao {
    private String databaseName;
    private int connectionNum;

    public BookDaoImpl(String databaseName, int connectionNum) {
        this.databaseName = databaseName;
        this.connectionNum = connectionNum;
    }

    public void save() {
        System.out.println("book dao save ..."+databaseName+","+connectionNum);
    }
}

七:构造器注入集合类型

构造方式注入constructor-arg标签内部也可以写<array>、<list>、<set>、<map>、<props>标签

八:依赖注入方式选择

1. 强制依赖 使用 构造器 进行,使用 setter 注入有概率不进行注入导致 null 对象出现
2. 可选依赖 使用 setter注入 进行,灵活性强
3. Spring 框架倡导使用构造器 第三方框架内部大多数采用构造器注入 的形式进行数据初始化,相对严谨
4. 如果有必要可以两者同时使用 ,使用构造器注入完成强制依赖的注入,使用 setter 注入完成可选依赖的注入
5. 实际开发过程中还要 根据实际情况分析 ,如果受控对象没有提供 setter 方法就必须使用构造器 注入
6. 自己开发的模块推荐使用 setter 注入

 九:依赖自动装配

        1.IoC容器根据bean所依赖的资源在容器中自动查找并注入到bean中的过程称为自动装配

            注意:自动装配用于引用类型依赖注入,不能对简单类型进行操作

                     自动装配优先级低于setter注入与构造器注入,同时出现时自动装配配置失效

        2.自动装配方式

                -按类型(配置中使用bean标签autowire属性设置自动装配的类型为byType)

            使用按类型装配时( byType )必须保障容器中相同类型的 bean 唯一,推荐使用
<bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl"/>
<bean id="bookService" class="com.itheima.service.impl.BookServiceImpl" autowire="byType"/>

                -按名称(配置中使用bean标签autowire属性设置自动装配的类型为byName)

                   使用按名称装配时(byName)必须保障容器中具有指定名称的bean,因变量名与配置耦合,不推荐使用

<bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl"/>
<bean id="bookService" class="com.itheima.service.impl.BookServiceImpl" autowire="byName"/>

                -按构造方法

                -不启动自动装配

   3.案例:

public interface BookDao {
    public void save();
}

public class BookDaoImpl implements BookDao {
    public void save() {
        System.out.println("book dao save ...");
    }
}

public interface BookService {
    public void save();
}

public class BookServiceImpl implements BookService{
    private BookDao bookDao;

    public void setBookDao(BookDao bookDao) {
        this.bookDao = bookDao;
    }

    public void save() {
        System.out.println("book service save ...");
        bookDao.save();
    }
}

<?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 class="com.itheima.dao.impl.BookDaoImpl"/>
    <!--autowire属性:开启自动装配,通常使用按类型装配-->
    <bean id="bookService" class="com.itheima.service.impl.BookServiceImpl" autowire="byType"/>

</beans>
public class AppForDIAutoware {
    public static void main( String[] args ) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        BookService bookService = (BookService) ctx.getBean("bookService");
        bookService.save();
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值