Java乔晓松-spring构造函数的注入以及null的注入

spring构造函数的注入

spring最原始的构造函数的注入方式是:

 

<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>


下面的是常用的spring构造函数的注入方式

 

 

1、根据构造器参数的类型

 

<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注入

 

 

 

<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、根据参数的名称注入

 

 

 

<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>

 

 



下面是我定义的2个类

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;

	}



	@Override

	public 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;

	}



	@Override

	public 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="教育部" />

	</bean>

</beans>


 

 

spring中null值的注入

 

 

<?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是字符串 -->

		<property name="name">

			<null />

		</property>

		<property name="dept">

			<null />

		</property>

	</bean>



</beans>


 

 





 

你可能感兴趣的:(spring)