review
In the previous article, we introduced two methods of dependency injection in Spring:
- Constructor injection, the main label is < constructor Arg / >
- Setter method injection. The main tag is < property / >
So the question is, how to inject ordinary types of dependencies, collection dependencies, and null s? Have a comprehensive understanding of this article.
Content of this article
- Common type and string injection
- Injection of reference type
- Injection of sets
- Internal bean injection
- null and empty string injection
- Composite attribute injection
Common type and string injection
Basic types and strings, specifying values directly through the value element. Last case, comparison class and configuration.
package com.crab.spring.ioc.demo03; /** * @author zfd * @version v1.0 * @date 2022/1/13 15:07 * @For me, please pay attention to the official account of crab Java notes for more technical series. */ public class SimpleBean { private String name; private int age; private float height; // Omit Getter Setter toString() }
<bean class="com.crab.spring.ioc.demo03.SimpleBean" id="simpleBean"> <property name="name" value="xxx"/> <property name="age" value="12"/> <property name="height" value="123.45f"/> </bean>
Injection of reference type
The reference type is set by the ref tag.
public class MyBean { private RefBean refBean; public void setRefBean(RefBean refBean) { this.refBean = refBean; } } public class RefBean { }
<!-- reference type--> <bean class="com.crab.spring.ioc.demo03.RefBean" id="refBean"/> <bean class="com.crab.spring.ioc.demo03.MyBean" id="myBean"> <property name="refBean" > <ref bean="refBean"/> </property> </bean>
Note: the local attribute of the ref element is no longer supported in 4.0 bean XSD because it no longer provides the value of a regular bean reference. When upgrading to 4.0 mode, change the existing ref local reference to ref bean. The following example
// Before 4.0 <property name="refBean" ref="refBean" > // Replace 4.0 with <property name="refBean" > <ref bean="refBean"/> </property>
Internal bean injection
The < bean / > element in the < property / > or < constructor Arg / > element defines an internal bean. Internal beans are always anonymous and are always created using external beans. Internal beans cannot be accessed independently or injected into other beans
<!-- inside bean Way of--> <bean class="com.crab.spring.ioc.demo03.MyBean" id="myBean"> <property name="refBean"> <bean class="com.crab.spring.ioc.demo03.RefBean"/> </property> </bean>
Injection of sets
The < List / >, < Set / >, < map / > and < props / > elements Set the Properties and parameters of Java collection types List, Set, map and Properties respectively< Map / > key or value, and the value of < Set / > can also be any of the following elements:
bean | ref | idref | list | set | map | props | value | null
Integrated case of set injection
A comprehensive example satisfies you.
package com.crab.spring.ioc.demo03; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; /** * @author zfd * @version v1.0 * @date 2022/1/13 15:53 * @For me, please pay attention to the official account of crab Java notes for more technical series. */ public class ComplexObject { //attribute private Properties adminEmails; //list private List<Object> someList; // map private Map<String, Object> someMap; // set private Set<Object> someSet; // Omit Getter Setter }
<!-- aggregate--> <bean class="com.crab.spring.ioc.demo03.ComplexObject" id="complexObject"> <property name="adminEmails"> <props> <prop key="administrator">administrator@example.org</prop> <prop key="support">support@example.org</prop> <prop key="development">development@example.org</prop> </props> </property> <!-- results in a setSomeList(java.util.List) call --> <property name="someList"> <list> <value>a list element followed by a reference</value> <ref bean="myBean" /> </list> </property> <!-- results in a setSomeMap(java.util.Map) call --> <property name="someMap"> <map> <entry key="an entry" value="just some string"/> <entry key="a ref" value-ref="myBean"/> </map> </property> <!-- results in a setSomeSet(java.util.Set) call --> <property name="someSet"> <set> <value>just some string</value> <ref bean="myBean" /> </set> </property> </bean>
Merge of collection elements
The Spring container supports merging collections. You can define < List / >, < map / >, < set / > or < props / > elements in the parent bean, and < List / >, < map / >, < set / > or < props / > elements in the child bean inherit from the parent set. That is, the value of the collection of the child bean is the result of merging the elements of the parent collection and the child collection, and the elements of the child collection overwrite the value specified in the parent collection. Note: the combination type should be consistent, such as List.
<!-- Collection merge--> <bean id="parent" abstract="true" class="com.crab.spring.ioc.demo03.ComplexObject"> <property name="adminEmails"> <props> <prop key="administrator">administrator@example.com</prop> <prop key="support">support@example.com</prop> </props> </property> </bean> <bean id="child" parent="parent"> <property name="adminEmails"> <!-- the merge is specified on the child collection definition --> <props merge="true"> <prop key="sales">sales@example.com</prop> <prop key="support">support@example.co.uk</prop> </props> </property> </bean>
The collection element specifies the type
The collection element types in the comprehensive case are all Object, and Spring also supports specifying more specific types, such as Integer. Take an example.
public class AccountBean { private Map<String, Float> accounts; public void setAccounts(Map<String, Float> accounts) { this.accounts = accounts; } }
<!-- The collection element specifies the type--> <bean id="accountBean" class="com.crab.spring.ioc.demo03.AccountBean"> <property name="accounts"> <map> <entry key="one" value="9.99"/> <entry key="two" value="2.75"/> <entry key="six" value="3.99"/> </map> </property> </bean>
null and empty string injection
null and empty string injection is very simple. Look at the case directly.
public class EmptyStringBean { private String name; private Integer code; // Omit Getter Setter }
<!-- `null`And empty string injection--> <bean class="com.crab.spring.ioc.demo03.EmptyStringBean"> <property name="name" value=""/> <property name="code"> <null></null> </property> </bean>
Composite attribute injection
Use compound or nested property names when setting bean properties. As long as all objects of the path (except the final property name) are not empty, null intermediate object will cause NullPointerException. Look at the case directly
public class NestedBean { private SimpleBean simpleBean; public SimpleBean getSimpleBean() { return simpleBean; } public void setSimpleBean(SimpleBean simpleBean) { this.simpleBean = simpleBean; } }
<!-- Composite attribute--> <bean class="com.crab.spring.ioc.demo03.NestedBean" id="nestedBean"> <property name="simpleBean.name" value="xxx"/> </bean>
summary
This article introduces how to inject various dependency attributes into bean s. The content is very detailed. It is recommended to write cases and practice and digest them. After all, a good memory is not as good as a bad pen. You can't swim when you stand on the shore.
Source code address: https://github.com/kongxubihai/pdf-spring-series/tree/main/spring-series-ioc/src/main/java/com/crab/spring/ioc/demo03
Knowledge sharing, please indicate the source of reprint. There is no order in learning, and the one who reaches is the first!