Java Qiao Xiaosong-injection of spring constructor and null Injection

Source: Internet
Author: User
Spring constructor Injection

Spring's most primitive constructor injection method is:

 

<bean id="employee" class="www.csdn.spring.cust.Employee"><constructor-arg value="qiao" /><constructor-arg value="20" /><constructor-arg ref="dept" /><constructor-arg value="nv" /></bean>

The following describes the injection methods of common spring constructor functions.

 

 

1. Based on the type of the constructor Parameter

 

<bean id="employee" class="www.csdn.spring.cust.Employee"><constructor-arg type="java.lang.String" value="qiao" /><constructor-arg type="java.lang.Integer" value="20" /><constructor-arg type="www.csdn.spring.cust.Dept" ref="dept" /><constructor-arg type="java.lang.String" value="nv" /></bean>

2. index-based injection

 

 

 

<bean id="employee" class="www.csdn.spring.cust.Employee"><constructor-arg index="0" value="qiao" /><constructor-arg index="1" value="20" /><constructor-arg index="2" ref="dept" /><constructor-arg index="3" value="nv" /></bean>

3. Inject according to the parameter name

 

 

 

<bean id="employee" class="www.csdn.spring.cust.Employee"><constructor-arg name="name" value="qiao" /><constructor-arg name="age" value="20" /><constructor-arg name="dept" ref="dept" /><constructor-arg name="sex" value="nv" /></bean>

 

 

The following are the two classes I have defined:

Dept. Java

package www.csdn.spring.cust;public class Dept {public String num;public String name;public void setNum(String num) {this.num = num;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "Dept [num=" + num + ", name=" + name + "]";}}

Employee. Java

 

 

 

package www.csdn.spring.cust;public class Employee {public String name;public Integer age;public Dept dept;public String sex;public Employee(String name, Integer age, Dept dept, String sex) {super();this.name = name;this.age = age;this.dept = dept;this.sex = sex;}@Overridepublic String toString() {return "Member [name=" + name + ", age=" + age + ", dept=" + dept+ ", sex=" + sex + "]";}}

Spring. xml

 

 

 

<? XML version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <! -- <Bean id = "employee" class = "www. csdn. spring. cust. employee "> <constructor-Arg value =" Qiao "/> <constructor-Arg value =" 20 "/> <constructor-Arg ref =" Dept "/> <constructor-Arg value = "NV"/> </bean> --> <! -- <Bean id = "employee" class = "www. csdn. spring. cust. employee "> <constructor-Arg type =" Java. lang. string "value =" Qiao "/> <constructor-Arg type =" Java. lang. integer "value =" 20 "/> <constructor-Arg type =" www. csdn. spring. cust. dept "ref =" Dept "/> <constructor-Arg type =" Java. lang. string "value =" NV "/> </bean> --> <! -- <Bean id = "employee" class = "www. csdn. spring. cust. employee "> <constructor-Arg Index =" 0 "value =" Qiao "/> <constructor-Arg Index =" 1 "value =" 20 "/> <constructor-Arg Index = "2" ref = "Dept"/> <constructor-Arg Index = "3" value = "NV"/> </bean> --> <bean id = "employee" class = "www. csdn. spring. cust. employee "> <constructor-Arg name =" name "value =" Qiao "/> <constructor-Arg name =" Age "value =" 20 "/> <constructor-Arg name = "Dept" ref = "Dept"/> <constructor-Arg name = "sex" value = "NV"/> </bean> <bean id = "Dept" class = "www. csdn. spring. cust. dept "> <property name =" num "value =" xx001 "/> <property name =" name "value =" Ministry of Education "/> </bean> </beans>

 

 

Injection of null values in spring

 

 

<? XML version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id = "employee" class = "www. csdn. spring. cust. employee "> <! -- Value = "null" null is a string --> <property name = "name"> <null/> </property> <property name = "Dept"> <null/> </property> </bean> </beans>

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.