關於Spring的注入方式

 

spring的三種注入方式:java


 

  1. 接口注入(不推薦
  2. getter,setter方式注入(比較經常使用
  3. 構造器注入(死的應用


關於getter和setter方式的注入spring

  •  autowire="defualt"
  •  autowire=「byName」
  •  autowire="bytype"

例如:有以下兩個類須要注入post

 1  package  org.jia;
 2 
 3  public   class  Order {
 4       private  String orderNum;
 5      @SuppressWarnings( " unused " )
 6       private  OrderItem orderitem;
 7 
 8       public  OrderItem getOrderitem() {
 9           return  orderitem;
10      }
11 
12       public   void  setOrderitem(OrderItem orderitem) {
13           this .orderitem  =  orderitem;
14      }
15 
16       public  String getOrderNum() {
17           return  orderNum;
18      }
19 
20       public   void  setOrderNum(String orderNum) {
21           this .orderNum  =  orderNum;
22      }
23      
24  }
25  package  org.jia;
26 
27  public   class  OrderItem {
28       private  String orderdec;
29 
30       public  String getOrderdec() {
31           return  orderdec;
32      }
33 
34       public   void  setOrderdec(String orderdec) {
35           this .orderdec  =  orderdec;
36      }
37  }
38 

getter&&setter方式第一種注入:this

<? 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"  class ="org.jia.OrderItem" >
        
< property  name ="orderdec"  value ="item00001" ></ property >
    
</ bean >
    
< bean  id ="order"  class ="org.jia.Order"   >
        <!-----注入變量 名字必須與類中的名字同樣------->
         < property  name ="orderNum"  value ="order000007" ></ property >
         <!--注入對象 名字爲orderitem,所屬的類的應用id爲orderItem-->
         < property  name ="orderitem"  ref ="orderItem" ></ property >
    
    -->
</ bean >


</ beans >

getter&&setter方式第二種注入: byNamespa

<? 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"  class ="org.jia.OrderItem" >
        
< property  name ="orderdec"  value ="item00001" ></ property >
    
</ bean >
    
< bean  id ="order"  class ="org.jia.Order"  autowire ="byName" >
        
< property  name ="orderNum"  value ="order000007" ></ property >
    
</ bean >
</ beans >

getter&&setter方式第三種注入:byTypexml

<? 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"  class ="org.jia.OrderItem" >
        
< property  name ="orderdec"  value ="item00001" ></ property >
    
</ bean >
    
< bean  id ="order"  class ="org.jia.Order"  autowire ="byType" >
        
< property  name ="orderNum"  value ="order000007" ></ property >
    
</ bean >
</ beans >



關於構造器的注入:

 autowire="constructor"
須要在Order.java中加入一個構造器
public Order(OrderItem item ){ orderitem = item; }對象

<? 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"  class ="org.jia.OrderItem" >
        
< property  name ="orderdec"  value ="item00001" ></ property >
    
</ bean >
    
< bean  id ="order"  class ="org.jia.Order"  autowire ="constructor" >
        
< property  name ="orderNum"  value ="order000007" ></ property >
    
</ bean >
</ beans >
相關文章
相關標籤/搜索