Spring:(二)DI依赖注入方式
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>
- spring——依赖注入DI(spring-test方式)
- spring——依赖注入DI(main函数方式)
- spring学习总结(三):IOC & DI 配置 Bean 之配置形式及依赖注入方式
- spring IOC(控制反转)和DI(依赖注入)以及三种依赖注入方式的比较
- Spring的依赖注入(DI)三种方式
- Spring2.5依赖注入方式(DI)
- Javaweb学习笔记之Spring的依赖注入方式
- SSH深度历险(八) 剖析SSH核心原理+Spring依赖注入的三种方式
- spring四种依赖注入方式
- spring依赖注入的好处以及三种注入的方式
- Spring使用 --- 基本概念(一):DI,依赖注入
- spring四种依赖注入方式
- 依赖注入(DI)的几种方式示例
- Spring依赖注入的三种方式详解之一:属性注入
- spring四种依赖注入的方式
- spring四种依赖注入方式
- spring的依赖注入DI(IOC)
- Spring详解(三)------DI依赖注入
- Spring的控制反转(IOC)和依赖注入(DI)的概念
- spring 依赖注入的5中方式