IOC是Spring中的核心部分,叫做控制反转活DI(依赖注入),看了很多大牛写的源码分析,表示看不懂,先简单的写一写应用,后面如果有看《Spring in action》,在来写些IOC的源码分析吧啊,未来的自己我看好你。
好,言归正传,下面来写些IOC的简单应用。
IOC一般可以通过2种方式来依赖注入,你需要的对象:一种是通过构造方法,二是通过Set方法,常用的是通过Set方法来实现。
下面来看:1,通过构造方法实现:
举个简单的例子,我们有个Dao接口,分别有Mysql和Oracl的实现里面有addUser()方法,UserMangerImpl实现了UserManger接口,对DAO 接口依赖。
我们使用Spring的Ioc注入DAO的具体实现到UserMangerImpl中,看下配置文件:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="Dao4MysqlImpl" class="com.lee.spring.dao.Dao4MysqlImpl"/>
<bean id="Dao4OracleImpl" class="com.lee.spring.dao.Dao4OracleImpl"/>
<bean id="UserMangerImpl" class="com.lee.spring.userManger.UserMangerImpl">
<constructor-arg ref="Dao4MysqlImpl"/>
</bean>
</beans>
使用标签bean来配置类,在UserMangerImpl中依赖注入Dao4MysqlImpl,<constructot-arg/>来表示使用构造函数来注入的。
2,使用Set方法来实现依赖注入:
还是来看下配置文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd" default-lazy-init="true">
<bean id="Dao4MysqlImpl" class="com.lee.spring.dao.Dao4MysqlImpl"/>
<bean id="Dao4OracleImpl" class="com.lee.spring.dao.Dao4OracleImpl"/>
<bean id="UserMangerImpl" class="com.lee.spring.userManger.UserMangerImpl" autowire="byName">
<property name="dao" ref="Dao4OracleImpl"></property>
</bean>
</beans>
中间有2个参数要注意下,一个是
default-lazy-init="true"
表示延迟加载
autowire="byName"
表示根据名字来自动转载
好下面在来个例子看下,怎么依赖注入普通属性:
我们看下这些普通属性怎么注入,看下配置文件:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd" default-autowire="byName">
<bean id="Bean1" class="com.lee.spring.Bean1">
<property name="strValue" value="Hello Spring"/>
<property name="intValue" value="123"/>
<!--
<property name="listValue">
<list>
<value>listValue1</value>
<value>listValue2</value>
</list>
</property>
-->
<property name="setValue">
<set>
<value>setValue1</value>
<value>setValue2</value>
</set>
</property>
<property name="mapValue">
<map>
<entry key="k1" value="v1"/>
<entry key="k1" value="v1"/>
</map>
</property>
<property name="stringValue">
<list>
<value>stringValue1</value>
<value>stringValue2</value>
</list>
</property>
</bean>
</beans>
好吧,今天就总结道这,有空再来。