Spring学习-------IOC

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>
好吧,今天就总结道这,有空再来。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值