---
title: "Spring的DI注入方式"
author: "wenbing2610"
date: "2012-03-28T20:51:00Z"
tags: ["Spring DI"]
categories: ["Web前端"]
slug: "spring-di"
draft: false
lastmod: "2012-03-28T20:51:00Z"
id: 1467366
url: "https://fy1368.iteye.com/blog/1467366"
---

依赖注入(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方式第一种注入：**

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

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

               &lt;/bean&gt;

&lt;/beans&gt;

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

**getter&&setter方式第三种注入：byType**

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