1. 接口注入(不推荐)
2. getter,setter方式注入(比较常用)
3. 构造器注入(死的应用)
关于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" 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>
方