Getter and setter Injection
· Autowire = "defualt"
· Autowire = "byName"
· Autowire = "bytype"
Detailed parsing Injection Method
For example, there are two classes to be injected:
First class:
Java code
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;
}
}
The second category:
Java code
Package org. jia;
Public class OrderItem {
Private String orderdec;
Public String getOrderdec (){
Return orderdec;
}
Public void setOrderdec (String orderdec ){
This. orderdec = orderdec;
}
}
Common getter & setter Methods
Method 1 injection:
Html code
<? 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">
<! ----- The injection variable name must be the same as the name in the class ------->
<Property name = "orderNum" value = "order000007"> </property>
<! -- The injection object is named orderitem and the Application id of the class is orderItem -->
<Property name = "orderitem" ref = "orderItem"> </property>
--> </Bean>
</Beans>
Method 2 injection: byName
Html code
<? Xml version = "1.0" encoding = "UTF-8"?>
<! DOCTYPE beans PUBLIC "-// SPRING // dtd bean // EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<Beans>
<! -- At this time, the id must be the same as the name of the OrderItem object defined in Order. java, otherwise it will not be found -->
<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>
Method 3 injection: byType
Html code
<? Xml version = "1.0" encoding = "UTF-8"?>
<! DOCTYPE beans PUBLIC "-// SPRING // dtd bean // EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<Beans>
<! -- ByType injection is irrelevant to the id. You can define the id as needed !!! But there cannot be multiple such IDs -->
<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"
Add a constructor to Order. java.
Java code
Public Order (OrderItem item)
{
Orderitem = item;
}
XML configuration file
Html code
<? 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>
Comparison of Three injection methods
Interface injection:
Because the interface injection mode is invasive, it requires that the component must be associated with a specific interface, so it is not optimistic and is used in reality.
Setter injection:
For programmers who are used to developing traditional javabean, it is more intuitive to set the dependency using the setter method.
If the dependencies are complex, the constructor that constructs the subinjection mode will also be quite large, while the set-value injection mode is more concise.
If a third-party class library is used, our component may be required to provide a default constructor. In this case, the subinjection mode is not applicable.
Constructor injection:
Complete a complete and legal object during construction. Www.2cto.com
All dependencies are presented in the constructor.
The dependency is set at one time by the container during construction. After the component is created, it remains in a relatively "unchanged" stable state.
Only the component creator cares about its internal dependency. For callers, the dependency is in the "black box.
Summary
Spring uses the injection method. Why does it use the injection method? In fact, Spring injection and IoC (I am talking about IoC) Reverse control are the same thing.
Theoretically: The third injection method (constructor injection) is more reasonable in terms of java usage, and the second injection method (setter injection) is used as a supplement.
Author: zhanghu198901