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)其实是同一个概念.当某个方法需要另外一个对象协助的时候,传统的方法就是有调 ...
随机推荐
- 【菜鸟玩Linux开发】在C++里操作MySQL
MySQL是一个的开源关系型数据库,对于服务端开发来说是一个优秀的选择.本篇内容将介绍如何在C++程序里操作MySQL数据库. ———————————————————————————————————— ...
- 【T-SQL基础】02.联接查询
概述: 本系列[T-SQL基础]主要是针对T-SQL基础的总结. [T-SQL基础]01.单表查询-几道sql查询题 [T-SQL基础]02.联接查询 [T-SQL基础]03.子查询 [T-SQL基础 ...
- PSP个人耗时
PSP2.1 Personal Software Process Stage Time(min) Planing 计划 20 #Estimate #估计这个任务需要多长时间 180 Developi ...
- 使用canvas检测HTML5视频解码错误
乍一看这标题,有点吊炸天的赶脚,canvas跟<video>能有什么联系?不过请放心我不是标题党.事情是这样的: HTML5的<video>标签所支持的视频格式确实有限,mp4 ...
- 在tomcat下部署工程
xx系统第一期工程完成,今天老大要我去部署系统,从来就没有在tomcat下部署过,一直都是在myeclipse下部署.启动.运行即可,所以这次遇到了几个问题,记录下来. tomcat启动 在安装tom ...
- Oracle 权限(grant、revoke)
200 ? "200px" : this.width)!important;} --> 数据库版本:11GR2 一.介绍 在oracle中没有其他数据库系统中的数据库的概念, ...
- [ACM_动态规划] 找零种类
问题描述:假设某国的硬币的面值有 1.5.10.50 元四种,输入一个金额 N (正整数,N<=1000),印出符合该金额的硬币组合有多少种. 问题分析: 1.5.10 元组合出 N 元的方法数 ...
- How to Change RabbitMQ Queue Parameters in Production?
RabbitMQ does not allow re-declaring a queue with different values of parameters such as durability, ...
- Redis学习笔记~Redis并发锁机制
回到目录 redis客户端驱动有很多,如ServiceStack.Redis,StackExchange.Redis等等,下面我使用ServiceStack.Redis为例,介绍一下在redis驱动中 ...
- DDD~我们应该知道的Model,DomainModel和ViewModel
回到目录 图在前 目前项目中可能出现的三种Model模式,对于我们现在开发的一个项目,我觉得使用DDD的思想来设计模型比较清晰,使用DDD的思想把模型model分成了如下三种: 下面是我微博中的截 ...
