DI 依赖注入

  DI(Dependency Injection)依赖注入,说简单一点就将类里面的属性在创建类的过程中给属性赋值,即将对象依赖属性(简单值,集合,对象)通过配置设值给该对象。

  

属性注入的方式

  •  构造方法的方式
  •  set方法的方式
  •  工厂方法注入

  主要学习前两种方式

构造方法的方式

  当是构造方法时注入Bean的属性值(简单值,集合,对象)

  利用<constructor-arg>标签进行属性的注入

    name:被设置属性的名

    value:被设置属性的值

 编写用构造方法的pojo

 package spring_test1.pojo;

 public class UserConstructor {
private String name;
private int id; public UserConstructor(String name, int id) {
super();
this.name = name;
this.id = id;
} @Override
public String toString() {
return "User_constructor [name=" + name + ", id=" + id + "]";
}
}

XML配置编写

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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"> <!-- Spring构造方法注入 -->
<bean name="user_cons" class="spring_test1.pojo.UserConstructor">
<constructor-arg name="name" value="Roy"/>
<constructor-arg name="id" value="1001"/>
</bean> </beans>

编写测试类

 package spring_test1.test;

 import static org.junit.Assert.*;

 import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import spring_test1.pojo.UserConstructor; public class UserConstructorTest { @Test
public void test() {
//创建Spring的工厂
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//得到User对象
UserConstructor userConstructor = (UserConstructor) applicationContext.getBean("user_cons");
System.out.println(userConstructor);
}
}

运行结果

set方法的方式

  我在Spring:(一)那一篇中的第一个Spring程序便是set方法时的属性注入方式

  利用<property>标签

    name:被设置属性的名

    value:被设置属性的值

标准XML格式

编写pojo

 package spring_test1.pojo;

 /**
* @author jyroy
*
*/
public class User {
private String name;
private int id; public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public String toString() {
return "User [name=" + name + ", id=" + id + "]";
}
}

编写XML配置文件

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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"> <!--Spring的set方法的属性注入-->
<bean name="user" class="spring_test1.pojo.User">
<property name="name" value="李东"/>
<property name="id" value="1007" />
</bean> </beans>

编写测试类

 package spring_test1.test;

 import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import spring_test1.pojo.User; public class UserTest { @Test
public void demo1() {
//创建Spring的工厂
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//得到User对象
User user = (User) applicationContext.getBean("user");
System.out.println(user);
}
}

运行结果

p命名空间的方式

  看上面的XML配置,似乎用<property/>标签还是比较臃肿。

  于是从2.0开始,Spring支持使用名称空间的可扩展配置格式。这些名称空间都是基于一种XML Schema定义。p命名空间就可以用bean 元素的属性代替<property/>元素。

  还需要在使用p命名空间时先声明使用对应的命名空间,即在bean元素上添加 xmlns:p="http://www.springframework.org/schema/p"

1         <!-- p命名空间的方式 -->
<bean id="user" class="spring_test1.pojo.User" p:name="Roy" p:id="1004"></bean>

c命名空间的方式

  C命名空间与p命名空间类似,但是使用c命名空间可以用内联的构造参数代替嵌套的constructor-arg元素

  同样先声明使用对应的命名空间,即在bean元素上添加 xmlns:c="http://www.springframework.org/schema/c"

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 标准XML格式 -->
<bean id="foo" class="x.y.Foo">
<constructor-arg name="bar" ref="bar"/>
<constructor-arg name="baz" ref="baz"/>
<constructor-arg name="email" value="foo@bar.com"/>
</bean> <!-- c命名空间格式 -->
<bean id="foo" class="x.y.Foo" c:bar-ref="bar" c:baz-ref="baz" c:email="foo@bar.com"/> <!-- 还可以使用c命名空间的参数索引格式 -->
<bean id="foo" class="x.y.Foo" c:_0-ref="bar" c:_1-ref="baz" c:_2="foo@bar.com"/> </beans>

SpEL表达式方式

  Spring 表达式语言 (Spring Expression Language),打算整理完整的一篇

集合类型属性注入

         <bean id="collectionBean" class="com.roy.spring.demo5.CollectionBean">
<!-- 数组类型 -->
<property name="arrs">
<list>
<value>数组一</value>
<value>数组二</value>
</list>
</property>
<!-- 注入list集合类型 -->
<property name="list">
<list>
<value>list一</value>
<value>list二</value>
</list>
</property> <!-- 注入set集合类型-->
<property name="set">
<set>
<value>set一</value>
<value>set二</value>
</set>
</property> <!-- 注入Map集合 -->
<property name="map">
<map>
<entry key="aaa" value="111"></entry>
<entry key="bbb" value="222"></entry>
<entry key="ccc" value="333"></entry>
</map>
</property> <!-- 注入property集合 -->
<property name="properties">
<props>
<prop key="key1">value1</prop>
<prop key="key2">value2</prop>
<prop key="key3">value3</prop>
</props>
</property>
</bean>

最新文章

  1. mybatis 批量更新
  2. SQL入门语句之LIKE、GLOB和LIMIT
  3. amgular $q用法
  4. MySQL的数据类型
  5. android wifi 获取扫描结果
  6. 区间DP lightoj 1422
  7. Mysql 调用存储过程的两种方式
  8. flatbuffers 使用问题记录
  9. 成为JavaGC专家(2)—如何监控Java垃圾回收机制
  10. 【转】G40-70、G50-70联想小新笔记本SR1000随机Linux改Windows 7系统操作指导
  11. UVA10817--状态压缩DP
  12. MongoDB C Driver and APIinstances linux MongoDB安装配置
  13. Leetcode: Median of Two Sorted Arrays. java.
  14. grunt live reload 配置记录
  15. Qt 之 入门例程(二)
  16. docker应用笔记
  17. flask中jinjia2模板引擎详解3
  18. WebService之soap类型的服务和rest类型的服务
  19. Vue.js与Jquery的比较 谁与争锋 js风暴
  20. Python基础(九) type元类

热门文章

  1. SDE与shapefile之间的数据导入与导出
  2. thinkphp---ajax实现删除
  3. thinkphp框架实现删除上传的文件
  4. Robot Framework之测试用例分层实战
  5. Axios源码深度剖析 - 替代$.ajax,成为xhr的新霸主
  6. ubuntu18.04安装安装JDK
  7. 使用crypto-js对数据进行AES加密、解密
  8. PAT1132: Cut Integer
  9. 页面读取Excel
  10. @component @bean区别