- <?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"<span style="color:#ff0000;"> autowire="byName"</span>>
- <property name="orderNum" value="order000007"></property>
- </bean>
- </beans>
方式第三种注入: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" <span style="color:#ff0000;">autowire="byType"</span>>
- <property name="orderNum" value="order000007"></property>
- </bean>
- </beans>
autowire="constructor"
需要在Order.java中加入一个构造器
- public Order(OrderItem item )
- {
- orderitem = item;
- }
XML配置文件
- <?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>
三种注入方式比较
接口注入:
接