莫小柒 发表于 2014-5-26 20:58

spring 如何实现依赖注入(构造方法|set注入)

构造方法注入
1、在类中添加一个构造方法
2、在spring配置文件中添加
<constructor-arg ref="userDaoOralceImpl"/>
<!--
              index只构造方法参数
              index默认是0(第一参数)
   -->
   <constructor-arg index="0">
           <ref bean="userDaoOralceImpl"/>
   </constructor-arg>
set注入
1、必须提供一个set方法
2、属性名建议小写
3、在spring配置文件配置<property></property>
   <!--<property name="iuserdao">
         <!-- iuserdao = new UserDaoOralceImpl-->
         <ref bean="userDaoOralceImpl"/>
       </property>
       -->
       <property name="iuserdao" ref="userDaoOralceImpl"/>

控制反转:--依赖注入使用控制反转实现
          使用容器管理对象
开发人员只需要关注业务逻辑就可以
让开发变的简单,后期维护简单

页: [1]
查看完整版本: spring 如何实现依赖注入(构造方法|set注入)