Spring框架第三篇之基于XML的DI注入
一、注入分类
Bean实例在调用无参构造器创建空值对象后,就要对Bean对象的属性进行初始化。初始化是由容器自动完成的,称为注入。根据注入方式的不同,常用的有两类:设值注入、构造注入、实现特定接口注入。由于第三种方式采用侵入式编程,污染代码,所以几乎不用。
1、设值注入
设值注入是指,通过setter方法传入被调用者的实例。这种注入方式简单、直观,因而在Spring的依赖注入中大量使用。
关于设值注入举个简单的例子:
分别创建一个学校类(School):
/**
* 学校类
*
* @author Root
*/
public class School { private String name; public void setName(String name) {
this.name = name;
} @Override
public String toString() {
return "School [name=" + name + "]";
} }
学生类(Student):
/**
* 学生类
*
* @author Root
*/
public class Student { private String name;
private int age;
// 对象属性,也叫做域属性
private School school; public Student() {
super();
} public void setName(String name) {
System.out.println("执行setName()");
this.name = name;
} public void setAge(int age) {
System.out.println("执行setAge()");
this.age = age;
} public void setSchool(School school) {
this.school = school;
} @Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", school=" + school + "]";
} }
在配置文件中使用设值注入方式,设值对象属性值:
<?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"> <!-- 注册School -->
<bean id="school" class="com.ietree.spring.basic.di.setter.School">
<property name="name" value="北京大学"></property>
</bean> <!-- 注册Student -->
<bean id="student" class="com.ietree.spring.basic.di.setter.Student">
<property name="name" value="李华"></property>
<property name="age" value="20"></property>
<!-- 域属性注入使用ref -->
<property name="school" ref="school"></property>
</bean> </beans>
注意:如果对象中包含有另外的对象引用,则需要使用ref,而不能使用value。
测试:
@Test
public void test01(){ String resource = "com/ietree/spring/basic/di/setter/applicationContext.xml";
ApplicationContext ctx = new ClassPathXmlApplicationContext(resource); Student student = (Student) ctx.getBean("student");
System.out.println(student);
}
程序输出:
执行setName()
执行setAge()
Student [name=李华, age=20, school=School [name=北京大学]]
2、构造注入
构造注入,顾名思义就是通过构造方法注入,举个简单的例子:
创建一个学校类(School):
/**
* 学校类
*
* @author Root
*/
public class School { private String name; public void setName(String name) {
this.name = name;
} @Override
public String toString() {
return "School [name=" + name + "]";
} }
学生类(Student):
/**
* 学生类
*
* @author Root
*/
public class Student { private String name;
private int age;
// 对象属性,也叫做域属性
private School school; /*public Student() {
super();
}*/ // 代参构造器
public Student(String name, int age, School school) {
super();
this.name = name;
this.age = age;
this.school = school;
} public void setName(String name) {
System.out.println("执行setName()");
this.name = name;
} public void setAge(int age) {
System.out.println("执行setAge()");
this.age = age;
} public void setSchool(School school) {
this.school = school;
} @Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", school=" + school + "]";
} }
配置文件:
<?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"> <!-- 注册School -->
<bean id="school" class="com.ietree.spring.basic.di.constructor.School">
<property name="name" value="北京大学"></property>
</bean> <!-- 注册Student -->
<!-- 方式一 -->
<!-- <bean id="student" class="com.ietree.spring.basic.di.constructor.Student">
<constructor-arg index="0" value="李华"/>
<constructor-arg index="1" value="20"/>
<constructor-arg index="2" ref="school"/>
</bean> --> <!-- 方式二 -->
<!-- <bean id="student" class="com.ietree.spring.basic.di.constructor.Student">
<constructor-arg value="李华"/>
<constructor-arg value="20"/>
<constructor-arg ref="school"/>
</bean> --> <!-- 方式三:推荐-->
<bean id="student" class="com.ietree.spring.basic.di.constructor.Student">
<constructor-arg name="name" value="李华"/>
<constructor-arg name="age" value="20"/>
<constructor-arg name="school" ref="school"/>
</bean> </beans>
个人强烈推荐使用方式三,因为这样的配置方式不会带来歧义,关键是可读性比强两者要强。
测试:
@Test
public void test01(){ String resource = "com/ietree/spring/basic/di/constructor/applicationContext.xml";
ApplicationContext ctx = new ClassPathXmlApplicationContext(resource); Student student = (Student) ctx.getBean("student");
System.out.println(student);
}
程序输出:
Student [name=李华, age=20, school=School [name=北京大学]]
二、命名空间注入
命名空间注入分为两种,p命名空间和c命名空间。
例:
创建School类:
/**
* 学校类
*
* @author Root
*/
public class School { private String name; public School(String name) {
super();
this.name = name;
} public void setName(String name) {
this.name = name;
} @Override
public String toString() {
return "School [name=" + name + "]";
} }
创建学生类(Student):
/**
* 学生类
*
* @author Root
*/
public class Student { private String name;
private int age;
// 对象属性,也叫做域属性
private School school; public Student() {
super();
} public Student(String name, int age, School school) {
super();
this.name = name;
this.age = age;
this.school = school;
} public void setName(String name) {
System.out.println("执行setName()");
this.name = name;
} public void setAge(int age) {
System.out.println("执行setAge()");
this.age = age;
} public void setSchool(School school) {
this.school = school;
} @Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", school=" + school + "]";
} }
配置文件:
<?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" xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 注册School -->
<!-- <bean id="mySchool" class="com.ietree.spring.basic.di.namespace.School" p:name="清华大学"/> 注册Student
<bean id="student" class="com.ietree.spring.basic.di.namespace.Student" p:name="李华" p:age="20" p:school-ref="mySchool"/> --> <!-- 注册School -->
<bean id="mySchool" class="com.ietree.spring.basic.di.namespace.School" c:name="清华大学"/> <!-- 注册Student -->
<bean id="student" class="com.ietree.spring.basic.di.namespace.Student" c:name="李华" c:age="20" c:school-ref="mySchool"/> </beans>
注意:
1、这里如果要想使用这两种命名空间方式的话,需要先导入约束:
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
2、如果是使用c命名空间的方式的话,在类里面还必须写上带参构造器。
三、集合属性注入
创建School类:
/**
* 学校类
*
* @author Root
*/
public class School { private String name; public void setName(String name) {
this.name = name;
} @Override
public String toString() {
return "School [name=" + name + "]";
} }
创建Some类:
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set; public class Some {
private School[] schools;
private String[] myStr;
private List<String> myList;
private Set<String> mySet;
private Map<String, Object> myMap;
private Properties myProps; public void setSchools(School[] schools) {
this.schools = schools;
} public void setMyStr(String[] myStr) {
this.myStr = myStr;
} public void setMyList(List<String> myList) {
this.myList = myList;
} public void setMySet(Set<String> mySet) {
this.mySet = mySet;
} public void setMyMap(Map<String, Object> myMap) {
this.myMap = myMap;
} public void setMyProps(Properties myProps) {
this.myProps = myProps;
} @Override
public String toString() {
return "Some [schools=" + Arrays.toString(schools) + ", myStr=" + Arrays.toString(myStr) + ", myList=" + myList
+ ", mySet=" + mySet + ", myMap=" + myMap + ", myProps=" + myProps + "]";
} }
配置applicationContext.xml文件:
<?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"> <!-- 注册School -->
<bean id="mySchool" class="com.ietree.spring.basic.di.list.School">
<property name="name" value="北京大学"></property>
</bean>
<bean id="mySchool2" class="com.ietree.spring.basic.di.list.School">
<property name="name" value="清华大学"></property>
</bean> <!-- 注册Student -->
<bean id="some" class="com.ietree.spring.basic.di.list.Some">
<property name="schools">
<array>
<ref bean="mySchool"/>
<ref bean="mySchool2"/>
</array>
</property>
<property name="myStr">
<array>
<value>中国</value>
<value>广东</value>
</array>
</property>
<property name="myList">
<list>
<value>深圳</value>
<value>龙岗</value>
</list>
</property>
<property name="mySet">
<set>
<value>坂田</value>
<value>天安云谷</value>
</set>
</property>
<property name="myMap">
<map>
<entry key="mobile" value="123456"></entry>
<entry key="weChat" value="654321"></entry>
</map>
</property>
<property name="myProps">
<props>
<prop key="edu">本科</prop>
<prop key="gender">性别</prop>
</props>
</property>
</bean> </beans>
方式二:
<?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"> <!-- 注册School -->
<bean id="mySchool" class="com.ietree.spring.basic.di.list.School">
<property name="name" value="北京大学"></property>
</bean>
<bean id="mySchool2" class="com.ietree.spring.basic.di.list.School">
<property name="name" value="清华大学"></property>
</bean> <!-- 注册Student -->
<bean id="some" class="com.ietree.spring.basic.di.list.Some">
<property name="schools">
<array>
<ref bean="mySchool"/>
<ref bean="mySchool2"/>
</array>
</property> <property name="myStr" value="中国,广东"/> <property name="myList" value="深圳,龙岗"/> <property name="mySet" value="坂田,天安云谷"/> <property name="myMap">
<map>
<entry key="mobile" value="123456"></entry>
<entry key="weChat" value="654321"></entry>
</map>
</property>
<property name="myProps">
<props>
<prop key="edu">本科</prop>
<prop key="gender">性别</prop>
</props>
</property>
</bean> </beans>
四、对于域属性的自动注入
<!-- 注册Student
autowire="byName":通过字段名注入
autowire="byType":通过字段类型注入
-->
<bean id="student" class="com.ietree.spring.basic.di.domain.Student" autowire="byName">
<property name="name" value="李华"></property>
<property name="age" value="20"></property>
</bean>
五、使用SPEL注入
SPEL,Spring Expression Language,即Spring EL表达式语言。即,在Spring配置文件中为Bean属性注入值时,可直接使用SPEL表达式计算的结果。SPEL表达式以#开头,后跟一对大括号。
举例:
创建Person类:
package com.ietree.spring.basic.di.spel;
public class Person {
private String pname;
private int page;
public Person() {
super();
}
public void setPname(String pname) {
this.pname = pname;
}
public void setPage(int page) {
this.page = page;
}
public String getPname() {
return pname;
}
public int getPage() {
return page;
}
public int computeAge() {
return this.page > 25 ? 25 : this.page;
}
@Override
public String toString() {
return "Person [pname=" + pname + ", page=" + page + "]";
}
}
创建Student类:
package com.ietree.spring.basic.di.spel; /**
* 学生类
*
* @author Root
*/
public class Student { private String name; private int age; public Student() {
super();
} public void setName(String name) {
this.name = name;
} public void setAge(int age) {
this.age = age;
} @Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
} }
配置ApplicationContext.xml文件:<?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="myPerson" class="com.ietree.spring.basic.di.spel.Person">
<property name="pname" value="李华"></property>
<!-- 随机生成0 - 99数字 -->
<property name="page" value="#{T(java.lang.Math).random() * 100}"></property>
</bean> <bean id="myStudent" class="com.ietree.spring.basic.di.spel.Student">
<property name="name" value="#{myPerson.pname}"></property>
<!-- <property name="age" value="#{myPerson.page > 25 ? 25 : myPerson.page}"></property> -->
<property name="age" value="#{myPerson.computeAge() }"></property>
</bean> </beans>
六、使用内部Bean注入
<?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"> <!-- 注册School -->
<!-- <bean id="school" class="com.ietree.spring.basic.di.inner.School">
<property name="name" value="北京大学"></property>
</bean> --> <!-- 注册Student -->
<bean id="student" class="com.ietree.spring.basic.di.inner.Student">
<property name="name" value="李华"></property>
<property name="age" value="20"></property>
<property name="school">
<bean class="com.ietree.spring.basic.di.inner.School">
<property name="name" value="清华大学"></property>
</bean>
</property>
</bean> </beans>
七、使用同类抽象Bean注入
<?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 -->
<bean id="baseStudent" class="com.ietree.spring.basic.di.sameextends.Student" abstract="true">
<property name="school" value="清华大学"/>
<property name="department" value="计算机系"/>
</bean>
<bean id="student1" parent="baseStudent">
<property name="name" value="小明"/>
<property name="age" value="21"/>
</bean>
<bean id="student2" parent="baseStudent">
<property name="name" value="张三"/>
<property name="age" value="22"/>
</bean>
<bean id="student3" parent="baseStudent">
<property name="name" value="李四"/>
<property name="age" value="23"/>
</bean> </beans>
八、使用异类抽象Bean注入
<?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 -->
<bean id="baseStudent" abstract="true">
<property name="school" value="清华大学"/>
<property name="department" value="计算机系"/>
</bean> <bean id="student" class="com.ietree.spring.basic.di.sameextends.Student" parent="baseStudent">
<property name="name" value="小明"/>
<property name="age" value="21"/>
</bean>
<bean id="teacher" class="com.ietree.spring.basic.di.sameextends.Teacher" parent="baseStudent">
<property name="name" value="李四"/>
<property name="age" value="23"/>
</bean> </beans>
九、为应用指定多个Spring配置文件
1、同级关系:

测试:
package com.ietree.spring.basic.di.mulxml; import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyTest { @Test
public void test01() { /*String resource1 = "com/ietree/spring/basic/di/setter/spring-base.xml";
String resource2 = "com/ietree/spring/basic/di/setter/spring-beans.xml";
ApplicationContext ctx = new ClassPathXmlApplicationContext(resource1, resource2);*/ String resource = "com/ietree/spring/basic/di/setter/spring-*.xml";
ApplicationContext ctx = new ClassPathXmlApplicationContext(resource); Student student = (Student) ctx.getBean("student");
System.out.println(student);
} }
2、包含关系:
<?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"> <import resource="classpath:com/ietree/spring/basic/di/mulxml/spring-base.xml"/>
<import resource="classpath:com/ietree/spring/basic/di/mulxml/spring-beans.xml"/>
</beans>
Spring框架第三篇之基于XML的DI注入的更多相关文章
- Spring框架第四篇之基于注解的DI注入
一.说明 与@Component注解功能相同,但意义不同的注解还有三个: 1)@Repository:注解在Dao实现类上 2)@Service:注解在Service实现类上 3)@Controlle ...
- Spring的第三天AOP之xml版
Spring的第三天AOP之xml版 ssm框架 spring AOP介绍 AOP(Aspect Oriented Programming),面向切面编程.它出来的目的并不是去取代oop,而是对它的 ...
- 深入学习Spring框架(三)- AOP面向切面
1.什么是AOP? AOP为 Aspect Oriented Programming 的缩写,即面向切面编程, 通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术..AOP是OOP的延续, ...
- Spring声明式事务管理(基于XML方式实现)
--------------------siwuxie095 Spring 声明式事务管理(基于 XML 方式实现) 以转账为例 ...
- spring3——IOC之基于XML的依赖注入(DI )
我们知道spring容器的作用是负责对象的创建和对象间关系的维护,在上一篇博客中我们讲到spring容器会先调用对象的无参构造方法创建一个空值对象,那么接下来容器就会对对象的属性进行初始化,这个初始化 ...
- SSM-Spring-07:Spring基于注解的di注入
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 注解: 说起注解,哇哦,每个人都或多或少的用到过 像什么@Overried,@Test,@Param等等之前就 ...
- Spring框架的事务管理之基于AspectJ的XML方式(重点掌握)
1. 步骤一:恢复转账开发环境(转账开发环境见“https://www.cnblogs.com/wyhluckdog/p/10137283.html”) 2.步骤二:引入AOP的开发包3.步骤三:引入 ...
- Spring中Bean的配置:基于XML文件的方式
Bean的配置一共有两种方式:一种是基于XML文件的方式,另一种是基于注解的方式.本文主要介绍基于XML文件的方式 <bean id="helloWorld" class=& ...
- Spring框架第五篇之Spring与AOP
一.AOP概述 AOP(Aspect Orient Programming),面向切面编程,是面向对象编程OOP的一种补充.面向对象编程是从静态角度考虑程序的结构,而面向切面编程是从动态角度考虑程序运 ...
随机推荐
- C语言 百炼成钢22
/* 题目58: 编写一个业务函数,实现按行读取文件.把内容按照第三种内存模型打包数据传出,把行数通过函数参数传出. 函数原型有两个,任意选择其一 要求1:请自己任意选择一个接口(函数),并实现功能: ...
- MyBatis常用对象SqlSessionFactory和SqlSession介绍和运用
学习框架一个比较好的路径阅读源码.本文介绍的SqlSessionFactory和SqlSession.可以通过了解SqlSessionFactory接口和SqlSession接口以及两个的实现类入手, ...
- php -- PDO预处理
可以使用多种方式实现预处理:指的是在绑定数据进行执行的时候,可以有多种方式. 预处理语句中为变量 使用数组指定预处理变量 1.准备预处理语句(发送给服务器,让服务器准备预处理语句) PDOStatem ...
- 【BZOJ】1684: [Usaco2005 Oct]Close Encounter(暴力+c++)
http://www.lydsy.com/JudgeOnline/problem.php?id=1684 这货完全在考精度啊.. 比如奇葩 (llf)a/b*i (llf)(a/b*i)和(llf)( ...
- centos7安装avahi
sudo yum install avahi sudo yum install avahi-tools 转自: http://unix.stackexchange.com/questions/1829 ...
- 开启mysql日志及若干问题
今天学习了mysql日志功能,以前也有所了解,只不过没有深入的学习,所以趁着“余热”,把我从网上找到的资料与实践 结合起来,总结一下其基本用法.学习从来都不是无趣的,就看你怎么看待学习. 1.查看查询 ...
- expected_conditions判断页面元素
expected_condtions提供了16种判断页面元素的方法: 1.title_is:判断当前页面的title是否完全等于预期字符串,返回布尔值 2.title_contains:判断当前页面的 ...
- cocos lua 加密方案
cocos2d使用的是luajit,lua原生编译出来的bytecode和luajit是不兼容的,所以直接用luac法编译出来的bytecode脚本无法在cocos2d中使用. 目前所指的解决方案有2 ...
- uva 110 Meta-Loopless Sorts 用程序写程序 有点复杂的回溯水题
题目要求写一个直接用比较排序的pascal程序,挺有趣的一题. 我看题目数据范围就到8,本来以为贪个小便宜,用switch输出. 然后发现比较次数是阶乘级别的,8的阶乘也是挺大的,恐怕会交不上去. 于 ...
- 比较好用的Opera 翻译工具 ddict
http://ddict.me