spring的三种注入方式:
接口注入(不推荐)
getter,setter方式注入(比较常用)
构造器注入(死的应用)
关于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方式第一种注入:
<!--注入对象 名字为orderitem,所属的类的应用id为orderItem-->
-->
getter&&setter方式第二种注入: byName
getter&&setter方式第三种注入:byType
关于构造器的注入:
autowire="constructor"
需要在Order.java中加入一个构造器
public Order(OrderItem item ){ orderitem = item; }