Spring的DI注入方式
?????????依赖注入(DI),是spring容器实现的基础,在spring-core模块中实现的。所谓DI,就是指对象是被动接受依赖类而不是自己主动去找,换句话说就是指对象不是从容器中查找它依赖的类,而是在容器实例化对象的时候主动将它依赖的类注入给它。DI注入的方式有:设置注入,接口注入,构造注入。
?
??????? ?关于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”>
???????????????????????? <property name=”orderdec” value=”item00001″></property>
????????????? </bean>
???????????? <bean id=”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”>
????????????????????????????<property name=”orderdec” value=”item00001″></property>
????????????????? </bean>
????????????????? <bean id=”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”>
??????????????????????????? <property name=”orderdec” value=”item00001″></property>
???????????????? </bean>
???????????????? <bean id=”order” autowire=”byType”>
?????????????????????????????<property name=”orderNum” value=”order000007″></property>
???????????????? </bean>
</beans>