关于Spring的注入方式

时间:2023-05-20 20:38:26

spring的三种注入方式:


  1. 接口注入(不推荐)
  2. getter,setter方式注入(比较常用)
  3. 构造器注入(死的应用)

关于getter和setter方式的注入:

  • autowire="defualt"
  • autowire=“byName”
  • autowire="bytype"

例如:有如下两个类需要注入

 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方式第一种注入:

<?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方式第二种注入: 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" 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方式第三种注入: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" 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>