spring 通过 xml 文件进行依赖注入

spring DI

这里所谓的注入, 也就是赋值的意思。 当我们创建了对象过后, 可以直接调用其函数了, 但是对于属性值还是需要赋值的。 这篇文章主要是一些注入方式的 demo。

开始之前, 我们需要知道, 在 spring 中, 为了模块与模块之间解耦, 我们对于变化的东西一般都写在配置文件中。 所谓变化的东西就是对象的生成以及对象的赋值(注入)。 一般我们把配置文件约定为叫 application**.xml, 放在 resource 目标文件夹下。

注入的方式

构造函数方法

实体类需要实现有参构造函数 xml 通过 constructor-arg 标签进行赋值 通过 name 或者 index 和具体的属性进行对应

<bean id="user2" class="com.msb.bean.user">
  <constructor-arg name="userId" value="1"/>
  <constructor-arg name="username" value="Tom"/>
</bean>

setter 方法注入

实体类需要实现 setter 方法 xml 文件通过 property 标签进行赋值

<bean id="user1" class="com.msb.bean.User">
  <property name="userId" value="i"/>
  <property name="username" value="Tom"/>
</bean>

空间注入 (p 命名空间或者 c 命名空间)

直接在 bean 标签里面对对象进行赋值, 但是需要添加命名空间的约束 p 命名空间就是对 property 标签的简化处理, c 命名空间就是对 constructor-arg 的 简化处理命名空间的具体值就是把 xmlns 的值中 beans 改为 c 或者 p

<?xml version="1.0" encoding="UTF-8"?>
<bean xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:c="http://www.springframework.org/schema/c"

<bean id="user1" class="com.msb.bean.User" p:userid="1" p:username="Tom"/>

<bean id="user1" class="com.msb.bean.User" c:userid="1" p:username="Tom"/>

不同类型属性的注入

基本类型属性的注入(赋值)。

基本类型就是直接在 bean 标签下面的 value 值这里赋值就可以了。 需要注意的是特殊值的写法比如 null 值, 是一个具体的标签 还要大于小于 and 符合 需要使用转义字符。< &lt (little than) ,> &gt (greater than), & &amp

引用类型属性的注入。

需要先让容器生成一个我们需要使用的引用类型对象。然后, 我们将引用类型属性的注入分为外部引用以及内部引用外部引用,将基本类型用的的 value 换为 ref 值是引用类 bean 的 id

<bean id="date1" class="java.util.Date"/>
<bean id="mouse1" class="com.msb.bean.Mouse">
  <property name="birthdate" ref="date1"/>
</bean>

内部引用, 用就是把引用类型的 bean 放入 property 中

<bean id="mouse1" class="com.msb.bean.Mouse">
  <property name="birthdate">
    <bean id="date1" class="java.util.Date"></bean>
  </property>
</bean>

集合类属性的注入

集合的注入需要导入 util 命名空间, 然后标签使用对应的 list , array , 放入 property 标签内 util 命名空间除了添加 xmlns:utils 以外, 还需要添加 xsi:schemaLocation.同时, 也有外部引用和内部引用的区别

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/util
       http://www.springframework.org/schema/util/spring-util.xsd
">

外部引用 一般用于集合对象, 就是一堆对象的集合

<util:list id="outerbookList">
  <bean id="b1" class="com.msb.bean.Book" p:bname="JAVA" p:author="msb"/>
  <bean id="b2" class="com.msb.bean.Book" p:bname="GO" p:author="msb"/>
</util:list>

<bean id="student" class="com.msb.bean.Student">
  <property name="bookList" ref="outerbookList"/>
</bean>

内部引用, 一般用于基础类型的集合

<bean id="student" class="com.msb.bean.Student">
  <property name="bookList">
    <array>
      <value>JAVA</value>
      <value>MySQL</value>
    </array>
  </property>
</bean>

Bean 的自动装配

Bean 的自动装配解决的是什么问题呢?

如果我们将一个 bean 对象注入另一个 bean 对象, 我们需要配置 使用 ref 关键字。 而现在有一种简单的方法, 来给一个 bean 对象注入另一个 bean 对象。

我们如何给对象自动装配呢?

autowire 有两个值 byName 和 byTypebyName 需要保证, 该类的属性名和需要注入的 bean 的 id 一致 byType 需要保证容器里面只有一个该 bean

<bean id="dept" class="com.msb.bean.Dept"></bean>
<bean id="emp" class="com.msb.bean.Emp" autowire="byName"></bean>

<bean id="dept" class="com.msb.bean.Dept"></bean>
<bean id="emp" class="com.msb.bean.Emp" autowire="byType"></bean>

外部属性文件给 bean 注入

我们如何通过外部文件给 bean 赋值

首先, 我们需要添加 context 命名空间 以及约束。然后, 我们 需要使用 context 标签的 property-placeholder 通过 location 的值确定属性文件位置最后, 使用 ${key}值来访问属性文件对应的值

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
     xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/beans/spring-context.xsd
">
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="username" value="${jdbc_username}"></property>
        <property name="password" value="${jdbc_password}"></property>
        <property name="url" value="${jdbc_url}"></property>
        <property name="driverClassName" value="${jdbc_driver}"></property>
    </bean>
</beans>


原文:spring 通过xml文件进行依赖注入_田镇珲_InfoQ写作社区