Spring的DI注入方式

         依赖注入(DI),是spring容器实现的基础,在spring-core模块中实现的。所谓DI,就是指对象是被动接受依赖类而不是自己主动去找,换句话说就是指对象不是从容器中查找它依赖的类,而是在容器实例化对象的时候主动将它依赖的类注入给它。DI注入的方式有:设置注入,接口注入,构造注入。

 

         关于getter和setter方式的注入:
         autowire=”defualt”
         autowire=“byName”
         autowire=”bytype”

例如:有如下两个类需要注入
         package org.jia;


         public class Order {


                   private String orderNum;
                   @SuppressWarnings(“unused”)
                   private OrderItem orderitem;


                   public OrderItem getOrderitem() {
                             return orderitem;
                  }
                  public void setOrderitem(OrderItem orderitem) {
                             this.orderitem = orderitem;
                  }
                  public String getOrderNum() {
                            return orderNum;
                  }
                  public void setOrderNum(String orderNum) {
                            this.orderNum = orderNum;
                  }
            }
          package org.jia;


          public class OrderItem {


                    private String orderdec;


                    public String getOrderdec() {
                               return orderdec;
                    }
                    public void setOrderdec(String orderdec) {
                               this.orderdec = orderdec;
                    }
           }
           getter&&setter方式第一种注入:


<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE beans PUBLIC “-//SPRING//DTD BEAN//EN” “http://www.springframework.org/dtd/spring-beans.dtd”>

<beans>
             <bean id=”orderItem”>
                         <property name=”orderdec” value=”item00001″></property>
              </bean>
             <bean id=”order” >
                           <!—–注入变量 名字必须与类中的名字一样——->
                           <property name=”orderNum” value=”order000007″></property>
                           <!–注入对象 名字为orderitem,所属的类的应用id为orderItem–>
                           <property name=”orderitem” ref=”orderItem”></property>

               </bean>

</beans>


getter&&setter方式第二种注入: byName
<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE beans PUBLIC “-//SPRING//DTD BEAN//EN” “http://www.springframework.org/dtd/spring-beans.dtd”>
<beans>
                 <!–此时的id就必须与Order.java中所定义的OrderItem的对象名称一样了,不然就会找不到–>
                <bean id=”orderitem”>
                            <property name=”orderdec” value=”item00001″></property>
                  </bean>
                  <bean id=”order” autowire=”byName”>
                             <property name=”orderNum” value=”order000007″></property>
                  </bean>
</beans>


getter&&setter方式第三种注入:byType


<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE beans PUBLIC “-//SPRING//DTD BEAN//EN” “http://www.springframework.org/dtd/spring-beans.dtd”>
<beans>
                 <!–按照byType注入则就与id没有关系,可以随便定义id !!!但是不能出现多个此类的id–>
                 <bean id=”orderitdfadafaem”>
                            <property name=”orderdec” value=”item00001″></property>
                 </bean>
                 <bean id=”order” autowire=”byType”>
                             <property name=”orderNum” value=”order000007″></property>
                 </bean>
</beans>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值