SpringIOC使用扩展
在上篇博客中,我们使用Spring通过setter访问器实现了对属性的赋值,这种做法被称为设值注入。除此之外Spring还提供了通过构造方法赋值的能力,成为构造注入。下面我们通过一个小demo来了解如何通过构造方法来注入值(因一个类中可能包含其他自定义类型的对象,所以我们采用Student类中包含Car类的实例来演示如何通过构造来给Student类属性注入值)
Student类:
public class Student {
private String name;//学生姓名
private Integer age; //学生年龄
private Car car;//学生的汽车
@Override
//重写toString()方法方便进行测试
public String toString() {
return "Student [name=" + name + ", age=" + age + ", car=" + car + "]";
}
//构造函数
public Student(String name, Integer age, Car car) {
System.out.println("我是带参构造");
this.name = name;
this.age = age;
this.car = car;
}
public Student() {
System.out.println("我是无参构造");
}
//属性访问器
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
}
Car类:
public class Car {
//汽车颜色
private String color;
//属性访问器
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
//重写toString()方法方便测试
@Override
public String toString() {
return "Car [color=" + color + "]";
}
}
applicationContext.xml配置:
<bean id="car" class="cn.wz.entity.Car">
<property name="color" value="白色"/>
</bean> <bean id="stu" class="cn.wz.entity.Student">
<!--通过constructor-arg元素向构造方法传入参数-->
<constructor-arg index="0" value="王哲"/>
<constructor-arg index="1" value="18"/>
<constructor-arg index="2" ref="car"/>
</bean>
测试代码:
public class Test {
public static void main(String[] args) {
hasArgumentConstructor();
}
public static void hasArgumentConstructor(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
Student student = context.getBean("stu",Student.class);
System.out.println(student);
}
}
最终运行结果:

使用p命名空间实现属性注入
Spring配置文件采用schema形式,使用不同的名命空间管理不同类型的配置,是的配置文件更具扩展性。而p命名空间的配置则不再使用property子元素来为对象的属性注入值,而是以Bean元素属性的方式来注入值如我们将上面demo的配置代码更改如下:
<bean id="car" class="cn.wz.entity.Car">
<property name="color" value="白色"/>
</bean>
<bean id="stu" class="cn.wz.entity.Student" p:name="王哲" p:age="18" p:car-ref="car" />
其测试用例运行效果是一样的,但需要注意的是在使用这种方式给属性注入值时一定要先导入P命名空间如图所示:
从上图中我们可以发现我们再xml配置文件的头文件中不但配置了P命名空间还有c命名空间,c命名空间是在使用构造注入值时使用其用法和P命名空间一样,这里不再进行演示了。
为集合属性注入值
对于存在属性类型为集合的对象Spring提供了list、set、map等元素对其进行配置。具体配置如下:
<!-- List集合 -->
<bean id="list" class="cn.wz.entity.Student">
<property name="list">
<list>
<value>王哲</value>
<value>张一铭</value>
</list>
</property>
</bean>
<!-- Set集合 -->
<bean id="set" class="cn.wz.entity.Student">
<property name="set">
<set>
<value>王哲</value>
<value>张一铭</value>
</set>
</property>
</bean>
<!-- map集合 -->
<bean id="map" class="cn.wz.entity.Student">
<property name="map">
<map>
<entry key="wz"><value>王哲</value></entry>
<entry key="zym"> <value>张一铭</value></entry>
</map>
</property>
</bean>
<!-- 数组 -->
<bean id="array" class="cn.wz.entity.Student">
<property name="array">
<list>
<value>王哲</value>
<value>张一铭</value>
</list>
</property>
</bean>
SpringIOC使用扩展的更多相关文章
- 【sping揭秘】10、SpringIOC容器扩展
关于component-scan操作(去除,失效) 这个spring中的配置项,可以扫描我们对应的包下面的类,自动把带上@component,@service,@controller, @reposi ...
- Log4j扩展使用--自定义输出
写在前面的话 log4j支持自定义的输出.所有的输出都实现了自Appender接口.一般来说,自定义输出值需要继承AppenderSkeleton类,并实现几个方法就可以了. 写这篇博客,我主要也是想 ...
- java~spring-ioc的使用
spring-ioc的使用 IOC容器在很多框架里都在使用,而在spring里它被应用的最大广泛,在框架层面 上,很多功能都使用了ioc技术,下面我们看一下ioc的使用方法. 把服务注册到ioc容器 ...
- 数据交换格式与SpringIOC底层实现
1.数据交换格式 1.1 有哪些数据交换格式 客户端与服务器常用数据交换格式xml.json.html 1.2 数据交换格式应用场景 1.2.1 移动端(安卓.iOS)通讯方式采用http协议+JSO ...
- 【转】Spring学习---SpringIOC容器的初始化过程
[原文]https://www.toutiao.com/i6594400249429623304/ SpringIOC容器的初始化过程 简单来说,IoC容器的初始化是由refresh()方法来启动的, ...
- Spring-IOC源码解读1-整体设计
1. SpringIOC提供了一个基本的javabean容器,通过IOC模式管理依赖关系,并通过依赖注入和AOP增强了为javabean这样的pojo对象赋予事务管理,生命周期管理等基本功能.2. S ...
- SpringIOC 二—— 容器 和 Bean的深入理解
上文:Spring IOC 一--容器装配Bean的简单使用 上篇文章介绍了 Spring IOC 中最重要的两个概念--容器和Bean,以及如何使用 Spring 容器装配Bean.本文接着记录 S ...
- SpringIoC和SpringMVC的快速入门
更多内容,欢迎关注微信公众号:全菜工程师小辉~ Spring的优势? 降低了组件之间的耦合性 ,实现了软件各层之间的解耦 可以使用容易提供的众多服务,如事务管理,消息服务等 容器提供单例模式支持 容器 ...
- springIOC及设计模式
一.IOC的概念: 控制反转(inversion of control)和依赖注入(dependency injection)其实是同一个概念.当某个方法需要另外一个对象协助的时候,传统的方法就是有调 ...
随机推荐
- 【Bugly干货】关于 Android N 那些你不知道的事儿
今年3月,Google 破天荒提前半年发布了 Android N 开发者预览版.当然,作为一个不合格的谷粉并没有第一时间体验安装,因为至今仍然能够回忆起来去年今日此门中(雾)兴冲冲刷了 Android ...
- ENode 1.0 - Staged Event-Driven Architecture思想的运用
开源地址:https://github.com/tangxuehua/enode 上一篇文章,简单介绍了enode框架的command service api设计思路.本文介绍一下enode框架对St ...
- Java语法糖3:泛型
泛型初探 在泛型(Generic type或Generics)出现之前,是这么写代码的: public static void main(String[] args) { List list = ne ...
- 必须知道的SQL编写技巧,多条件查询不拼字符串的写法
在做项目中,我们经常遇到复杂的查询方法,要根据用户的输入,判断某个参数是否合法,合法的话才能当作过滤条件,我们通常的做法是把查询SQL赋值给一个字符串变量,然后根据判断条件动态的拼接where条件进行 ...
- Android多线程分析之二:Thread的实现
Android多线程分析之二:Thread的实现 罗朝辉 (http://www.cnblogs.com/kesalin/) CC 许可,转载请注明出处 在前文<Android多线程分析之一 ...
- [HIMCM暑期班]第1课:概述
作为这个系列的开始,我会把每一节课上过的内容,与同学们互动后发现他们的闪光点记录下来,以后其他要准备该比赛的人借鉴和参考. 第一节课是概述,主要讲什么是数学建模,还有建模可以帮助我们做什么.举了三个例 ...
- C#高级二
编程小虾米大侠之梦 软件环境:win7 开发工具:vs 2010 平台:.NET 语言:C# 类库版本:.NET Framework 2.0 语言特点:强类型语言 规划知识点: 1..net fram ...
- 理解nginx的配置
Nginx配置文件主要分成四部分:main(全局设置).server(主机设置).upstream(上游服务器设置,主要为反向代理.负载均衡相关配置)和 location(URL匹配特定位置后的设置) ...
- JS基础知识总结
js基础知识点总结 如何在一个网站或者一个页面,去书写你的js代码:1.js的分层(功能):jquery(tool) 组件(ui) 应用(app),mvc(backboneJs)2.js的规划() ...
- react3 组件
<body><!-- React 真实 DOM 将会插入到这里 --><div id="example"></div> <!- ...
