前文已经介绍Spring IOC&DI主要解决了对象和对象之间的耦合问题,将每一个对象作为bean交给Spring容器来管理。本文主要总结Spring IOC&DI的具体应用,包括其xml配置文件写法、依赖注入方式、bean获取方式等。
既然是解决对象和对象之间的耦合,那根据所依赖对象的类型可以分为:
(1)基本类型对象:所依赖对象为基本类型对象。如:int、String等
(2)自定义类型:所依赖对象为其他的自定义的类类型。
(3)集合类型:所依赖对象为集合类型,如List、Set、Map、Property等
(4)null 空值
同时根据依赖注入的方式分为两种方式:构造器注入和setter注入(接口注入已基本被废弃)
还是以上一节的例子。
1、构造器注入方式
(1) 基本类型对象注入
假设在dao的实现类中有一个属性用来记录具体的数据库的名字,则定义如下:
package com.springframework.ioc; public class MysqlDaoImpl implements MyDao{ private String name; public MysqlDaoImpl(String name){ this.name = name; } @Override public void addUser(){ System.out.println("this is " + name + " add user..."); } }
需要将name通过构造函数形式注入进来。
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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd" default-lazy-init="true"> <!-- beans declare go here --> <bean id="mysql" class="com.springframework.ioc.MysqlDaoImpl"> <constructor-arg value="mysql"/> </bean> <!-- 通过含参构造器注入 --> <bean id = "myservice" class="com.springframework.ioc.MyService"> <constructor-arg ref = "mysql"/> </bean> </beans>
如果构造函数中有两个参数呢,假设现在又加了一个id的参数。
package com.springframework.ioc; public class MysqlDaoImpl implements MyDao{ private String name; private int id; public MysqlDaoImpl(String name, int id){ this.name = name; this.id = id; } @Override public void addUser(){ System.out.println("this is " + name "id is " + id + " add user..."); } }
这个时候可以继续以上面的方式配置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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd" default-lazy-init="true"> <!-- beans declare go here --> <bean id="mysql" class="com.springframework.ioc.MysqlDaoImpl"> <constructor-arg value="mysql"/> <constructor-arg value="1"/> </bean> <!-- 通过含参构造器注入 --> <bean id = "myservice" class="com.springframework.ioc.MyService"> <constructor-arg ref = "mysql"/> </bean> </beans>
会输出正确的结果,
this is mysql id is 1 add user...
但是如果我调换一下顺序,变成这样:
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd" default-lazy-init="true"> <!-- beans declare go here --> <bean id="mysql" class="com.springframework.ioc.MysqlDaoImpl"> <constructor-arg value="1"/> <constructor-arg value="mysql"/> </bean> <!-- 通过含参构造器注入 --> <bean id = "myservice" class="com.springframework.ioc.MyService"> <constructor-arg ref = "mysql"/> </bean> </beans>
此时因为类型不匹配会报错,但是如果我两个参数都是String类型的,就会傻傻分不清楚了,所以,为了能够分清楚,一般会如下设置,指定参数的index:
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd" default-lazy-init="true"> <!-- beans declare go here --> <bean id="mysql" class="com.springframework.ioc.MysqlDaoImpl"> <constructor-arg index="1" value="1"/> <constructor-arg index="0" value="mysql"/> </bean> <!-- 通过含参构造器注入 --> <bean id = "myservice" class="com.springframework.ioc.MyService"> <constructor-arg ref = "mysql"/> </bean> </beans>
这样就可以清楚的知道注入的是哪一个参数。
(2) 自定义类型
自定义类型即所依赖的对象为自定义的类,比如在上一条中介绍的MyService类依赖MysqlDaoImpl类,在xml配置文件中配置如下:
</bean> <!-- 通过含参构造器注入 --> <bean id = "myservice" class="com.springframework.ioc.MyService"> <constructor-arg ref = "mysql"/> </bean>
如果有两个参数都是自定义类型,则可以按照以上针对基本类型的做法,设置构造器的index属性。如下:
<bean id = "myservice" class="com.springframework.ioc.MyService"> <constructor-arg index="0" ref = "mysql"/> <constructor-arg index="1" ref = "description"/> </bean>
其中description是另外一个引用的bean的id。
(3)集合类型
总体概括有四种集合类型:List、Set、Map、Props。
集合中可以存放基本类型,也可以存放自定义类型。
存放基本类型的xml配置如下,分别有List、Set和Map类型,其中List、Set类型容器中存放的是String类型数据,Map类型容器中键值为String类型,值为Integer类型数据:
<bean id = "myservice" class="com.springframework.ioc.MyService"> <constructor-arg index="0" ref = "mysql"/> <constructor-arg index="1" ref = "description"/> <constructor-arg index="2"> <list> <value>xiaoming</value> <value>xiaohong</value> <value>xiaolin</value> </list> </constructor-arg> <constructor-arg index="3"> <set> <value>jin</value> <value>yin</value> <value>yin</value> </set> </constructor-arg> <constructor-arg index="4"> <map> <entry key="wang" value="1"/> <entry key="li" value="2"/> <entry key="hu" value="3"/> </map> </constructor-arg> </bean>
Props与Map类型的容器差不多,只是Props只能存放String类型的键和值。
如果容器中存放的是自定义类型,xml文件中的配置如下:
<bean id = "myservice" class="com.springframework.ioc.MyService"> <constructor-arg index="0" ref = "mysql"/> <constructor-arg index="1" ref = "description"/> <constructor-arg index="2"> <list> <ref bean="bean1"> <ref bean="bean2"> <ref bean="bean3"> </list> </constructor-arg> <constructor-arg index="3"> <set> <ref bean="bean1"> <ref bean="bean2"> <ref bean="bean3"> </set> </constructor-arg> <constructor-arg index="4"> <map> <entry key="wang" value-ref="bean7"/> <entry key-ref="bean10" value-ref="bean8"/> <entry key="hu" value-ref="bean9"/> </map> </constructor-arg> </bean>
(4)null空值
如果想要给某一个类型赋空值,xml配置文件配置如下:
<constructor-arg index="0"> <null/> </constructor-arg>
相关推荐
今天有空,写了基于C#使用Spring.Net的演示实例,希望能给有需要的人带来帮助,其中演示了配置下的IOC、AOP、属性注入、构造函数注入、通知过滤器、以及不使用配置直接代码硬编的AOP动态代码过程,另外还增加了...
(十)构造函数注入 (十一)set方法注入 (十二)注入复杂类型数据 (十三)业务层实现类调用持久层的方法 (一)IOC的概念和作用 IOC:Inversion of Control,是控制反转的意思 我们之前想new谁就new谁,控制权在
Spring基础 题⽬ 01- 请你谈谈⾃⼰对于 IoC 和 AOP 的理解 1. IoC IoC的本质其实就是⼀个Map,...也就是通过构造函数和set⽅法的⽅式,去为每个容器实例化注⼊所需要 依赖的对象。为我们⾃动地绑定每个实例化对象的依
常用注解实践示例、问答S02 - 依赖注入Spring @Component 模型DI 类型 - 构造函数、setter、p 和 c 命名空间Bean 作用域实践示例、问答S03 - 数据访问 - 第一部分数据源绑定,连接池配置JDBCTemplate 介绍工具 - ...
在对由三部分组成的 Spring 系列 的第 1 部分进行总结时,我使用了一个示例,演示了如何通过 Spring IOC 容器注入应用程序的依赖关系(而不是将它们构建进来)。 我用开启在线信用帐户的用例作为起点。对于该实现,...
Hiboot-Web / CLI应用程序框架 关于 Hiboot是用Go编写的云原生Web和cli应用程序框架。 Hiboot并没有尝试重塑一切,它集成了流行的... 具有结构标记名称“ inject:”” ,构造函数或Method的依赖项注入。 入门 社区贡
作为面向对象编程中实现控制反转(Inversion of Control,下文称IoC)最常见的技术手段之一,依赖注入(Dependency Injection,下文称DI)可谓在OOP编程中大行其道经久不衰。比如在J2EE中,就有大名鼎鼎的执牛耳者...
作为面向对象编程中实现控制反转(Inversion of Control,下文称IoC)最常见的技术手段之一,依赖注入(Dependency Injection,下文称DI)可谓在OOP编程中大行其道经久不衰。比如在J2EE中,就有大名鼎鼎的执牛耳者...
17、构造器Constructor 是否可被override? 9 18、两个对象值相同(x.equals(y) == true),但却可有不同的hash code,这句话对不对? 9 19、是否可以继承String 类? 9 20、以下二条语句返回值为true 的有: 10 21、当一...
17、构造器Constructor 是否可被override? 9 18、两个对象值相同(x.equals(y) == true),但却可有不同的hash code,这句话对不对? 9 19、是否可以继承String 类? 9 20、以下二条语句返回值为true 的有: 10 21、当一...
静态构造函数 忽略大小写Replace效率瓶颈IndexOf 随机排列算法 理解C#中的委托[翻译] 利用委托机制处理.NET中的异常 与正则表达式相关的几个小工具 你真的了解.NET中的String吗? .NET中的方法及其调用(一) 如何...
目录 ... 11 2、CSS样式定义优先级顺序是? 12 3、div和span的区别? 12 4、CSS选择器包括? 12 5、用css3语法中,如何实现一个矩形框的圆角效果和50%红色透明效果?,请写出关键脚本 12 ...24、谈谈Spring的IOC和DI