There are two types of injection, as follows:
- Set Injection
- Construction Injection
Set Injection Details
Use the <property>tag.
Injection assignments to member variables.For different types of member variables.Other tags can be nested within <property>tags.
Membership variables fall into two categories:
- JDK built-in type
- User-defined Types
JDK built-in type
JDK built-in types, types defined by Java itself such as:
int
String[]
Set<String>
List<String>
Map<String>
Properties
How do I assign values?The following are discussed separately.
- String + 8 basic types (char, boolean, byte, short, int, long, float, double)
Use the <value>tag.
// private int id; <property name="id"> <value>10</value> </property>
// private String name; <property name="name"> <value>Adam</value> </property>
- Array type, need to add <list>label.
// private String[] emails; <property name="emails"> <list> <value>[email protected]</value> <value>[email protected]</value> <value>[email protected]</value> </list> </property>
- set collection
Configuration collection, set collection is out of order, not duplicated.
The <set>tag is the core of the set collection.Each type of label is nested inside.The <value> tag is here because a generic type of set <String> String is defined.
// private Set<String> tels; <property name="tels"> <set> <value>13231323123</value> <value>13231323123</value> <value>13661366166</value> </set> </property> //Output has only two values, set will filter and adjust duplicate values, not duplicate, and out of order //13231323123 //13661366166
- List Collection
Ordered, repeatable, use <list>tags, which can nest other tags.
// private List<String> addresses; <property name="addresses"> <list> <value>cpark</value> <value>askin</value> <value>partington</value> <value>askin</value> </list> </property> //ouput: //cpark //askin //partington //askin
- Map Collection
A <map> <entry> <key> tag is required, and the value selects the tag based on the corresponding type.
There is a key``value pair in the <entry>label.
The key needs to be marked with a <key>tag, and the generic type of the key is String, so a <value>tag is nested.
Value has no special label, and the generic type of value is String type labeled with <value>
// private Map<String,String> maps; <property name="maps"> <map> <entry> <key><value>key0</value></key> <value>123123</value> </entry> <entry> <key><value>key1</value></key> <value>456456</value> </entry> </map> </property>
- Properties type
The Properties type is a special map.All key value s are of type String, key = String value=String
Use the <props>and <prop>tags.
A <prop>tag represents a key-value pair.
The key is written in the key property,
Values written directly between <prop> </prop> tags are string types themselves so the <value> tag is omitted.
// private Properties p; <property name="p"> <props> <prop key="user">value1</prop> <prop key="user2">value2</prop> </props> </property>
User-defined Types
- Provide set get methods for users
However, you still need to create set get methods because Spring automatically calls these functions when the underlying reflection is created.
What we mean by decoupling is not that we don't need these functions, but that when we assign or update object properties, we don't need to modify them in the code (they need to be recompiled in the code). Instead, we modify the configuration file and don't need to recompile the code.
package com.gogogo; public class UserServiceImpl implements UserService{ private UserDAO userDAO; public UserDAO getUserDAO() { return userDAO; } public void setUserDAO(UserDAO userDAO) { this.userDAO = userDAO; } }
- Injection in configuration file (assignment)
The <property>property is the set value for our member variable.
//public class UserServiceImpl implements UserService{ // private UserDAO userDAO; //} // <bean id="userService"> <property name="userDAO"> <bean/>// Use only once, omit id configuration here </property> </bean>
- Second way of assignment
As we will see below, the code is cumbersome and the userDAO is recreated each time, taking up memory.
<bean id="userService"> <property name="userDAO"> <bean></bean> </property> </bean> <bean id="orderService"> <property name="userDAO"> <bean></bean> </property> </bean>
Solution:
Create userDAO first to add id attribute for easy invocation.
<bean id="userDAO"></bean>
Use <ref bean="bean_ID'> Reference userDAO member variable.
<bean id="userService"> <property name="userDAO"> //Member Property Name <ref bean="userDAO"></ref> // Reference to the bean s defined above </property> </bean>
<bean id="orderService"> <property name="userDAO"> //Member Property Name <ref bean="userDAO"></ref> // Reference to the bean s defined above </property> </bean>
Set Injection Simplified Writing
- Attribute-based simplification
- JDK Type Injection
The value attribute replaces the <value>tag.
Note: Only String type + 8 basic types (char, boolean, byte, short, int, long, float, double) can be used to simplify <value>tags.
//<property name="id"> // <value>10</value> //</property>--> <property name="id" value="10"></property> //<property name="name"> // <value>Cristina</value> //</property>--> <property name="name" value="Cristina"></property>
- User-defined Types
The ref attribute replaces the <ref>tag.
<bean id="userDAO"></bean> //<bean id="userService"> // <property name="userDAO"> // <ref bean="userDAO"></ref> // </property> //</bean> <bean id="userService"> <property name="userDAO" ref="userDAO"> </property> </bean>
- Namespace p-based simplification
The new namespace p:name="xiaowang" p:id="3" omits the subsequent assignments and needs to be introducedXmlns:p="Http://www.springframework.org/schema/p"
The format is as follows:
<bean id="" p:name="" p:id ="" />
<bean id="" p:userDAO-ref=""></bean>
<bean id="person" p:name="xiaowang" p:id="3"> // <property name="id" value="10"></property> // <property name="name" value="Cristina"></property> </bean>
//<bean id="userService"> // <property name="userDAO" ref="userDAO"> // </property> //</bean> <bean id="userService" p:userDAO-ref="userDAO"/>
Construction Injection
Spring calls the construction method to assign values to member variables through a configuration file.Use the <constructor-arg>tag.
- Development steps
- Provides a reference construction method
package com.gogogo.constructer; import java.io.Serializable; public class Customer implements Serializable { private String name; private int age; public Customer(String name, int age) { this.name = name; this.age = age; } @Override public String toString() { return "Customer{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
- Provide a configuration file
Because no set injection does not provide a set method, the <Property>tag cannot be used in the configuration file, the <constructor-arg>tag can be used for construction injection, and a <constructor-arg>tag can be used to inject a property in the same order as the member properties defined in the class, otherwise the compilation will be saved.
// private String name; // private int age; <bean id="customer"> <constructor-arg name="name" value="jake"></constructor-arg> <constructor-arg name="age" value="22"></constructor-arg> </bean>
test result
public void Test4(){ // Get factories ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml"); //Get Objects Customer customer = (Customer) ctx.getBean("customer"); System.out.println("customer: " + customer); } // ouput // customer: Customer
Construction method overload
Function names are the same (different number of parameters, different parameter types)
- Different number of parameters
Handle by controlling the number of <constructor-arg>tags - Same number of construction parameters
Specify type, introduce type attribute to distinguish
Set injection uses the <property>tag
Construct injections use <constructor-arg>tags in the same order
Set injection is used more in practice, construction injection takes into account overload and is cumbersome. Set injection is also used extensively at the bottom of Spring Framework.
Oliver to
Wishing you a happy studyIn this paper,