非集合依赖注入的三种方式:
private int stuNo;
private String stuName;
private int stuAge;
private Teacher teacher;
public Student() {
}
public Student(int stuNo, String stuName, int stuAge, Teacher teacher) {
super();
this.stuNo = stuNo;
this.stuName = stuName;
this.stuAge = stuAge;
this.teacher = teacher;
}
//set、get....
public String toString() {
return this.stuNo+","+this.stuName+","+this.stuAge+","+this.teacher;
}
private String teacherName;
private String teacherSex;
public Teacher() {
}
//set、get...
public String toString() {
return "{"+this.teacherName+","+this.teacherSex+"}";
}
方法一、set注入:
通过<property></property>注入,默认使用的是setXxx()方法赋值,底层通过反射实现。
注意:简单类型:value= "",引用类型:value=" 引用的id"
<bean id="teacher" class="org.lanqiao.entity.Teacher" p:teacherName="chinese" p:teacherSex="boy">
</bean>
<bean id="student" class="org.lanqiao.entity.Student">
<property name="stuNo" value="2"></property>
<property name="stuName" value="ls"></property>
<property name="stuAge" value="24"></property>
<property name="teacher" ref="teacher"></property>
</bean>
//2,ls,24,{chinese,boy}
方法二、构造器注入:
注意:简单类型:value= "",引用类型:value=" 引用的id"
1)、顺序注入
根据构造方法变量顺序赋值,例如:public Student(int stuNo, String stuName, int stuAge).
<bean id="teacher" class="org.lanqiao.entity.Teacher" p:teacherName="chinese" p:teacherSex="boy">
</bean>
<bean id="student" class="org.lanqiao.entity.Student">
<constructor-arg value="3"></constructor-arg>
<constructor-arg value="w5"></constructor-arg>
<constructor-arg value="25"></constructor-arg>
<constructor-arg ref="teacher"></constructor-arg>
</bean>
//3,w5,25,{chinese,boy}
2)、乱序注入
通过标识符(name、type、index)乱序注入.
<bean id="teacher" class="org.lanqiao.entity.Teacher" p:teacherName="chinese" p:teacherSex="boy">
</bean>
<bean id="student" class="org.lanqiao.entity.Student">
<constructor-arg name="stuNo" value="4"></constructor-arg>
<constructor-arg name="stuAge" index="2" value="25"></constructor-arg>
<constructor-arg type="String" value="z6"></constructor-arg>
<constructor-arg ref="teacher"></constructor-arg>
</bean>
//4,2,z6,{chinese,boy}
方法三、p命名空间注入(默认使用setXxx()方法注入)
必须引入p命名空间:xmlns:p="http://www.springframework.org/schema/p"
注意:多个 p赋值的时候 要有空格。
简单类型-> p:属性名="属性值"
引用类型(除了String外)->p:属性名-ref="引用的id"
<bean id="teacher" class="org.lanqiao.entity.Teacher" p:teacherName="chinese" p:teacherSex="boy">
</bean>
<bean id="student" class="org.lanqiao.entity.Student" p:stuName="a1" p:stuNo="5" p:stuAge="26"
p:teacher-ref="teacher">
</bean>
//5,a1,26,{chinese,boy}
集合依赖注入(其他方法同上):
public class AllCollectionType {
private List<String> listElement ;
private String[] arrayElement ;
private Set<String> setElement ;
private Map<String,String> mapElement;
private Properties propsElement ;
//set、get...
//可利用构造器注入
public AllCollectionType(List<String> listElement) {
this.listElement = listElement;
}
@Override
public String toString() {
//["aa","bb","cc"]
String strContent = "" ;
for(String str :arrayElement) {
strContent += str +"," ;//aa,bb,cc,
}
return "list:"+this.listElement+"\nset:"+this.setElement+"\nmap:"+this.mapElement+"\nprop:"+this.propsElement +"\narray:"+strContent;
}
}
1)利用setXxx()注入:
<bean id="collectionDemo" class="org.lanqiao.entity.AllCollectionType">
<property name="listElement">
<list>
<value>足球</value>
<value>篮球</value>
<value>乒乓球</value>
</list>
</property>
<property name="arrayElement">
<array>
<value>足球1</value>
<value>篮球1</value>
<value>乒乓球1</value>
</array>
</property>
<property name="setElement">
<set>
<value>足球2</value>
<value>篮球2</value>
<value>乒乓球2</value>
</set>
</property>
<property name="mapElement">
<map>
<entry>
<key>
<value>foot</value>
</key>
<value>足球3</value>
</entry>
<entry>
<key>
<value>basket</value>
</key>
<value>篮球3</value>
</entry>
<entry>
<key>
<value>pp3</value>
</key>
<value>乒乓球3</value>
</entry>
</map>
</property>
<property name="propsElement">
<props>
<prop key="foot4">足球4</prop>
<prop key="basket4">篮球4</prop>
<prop key="pp4">乒乓球4</prop>
</props>
</property>
</bean>
打印结果:
list:[足球, 篮球, 乒乓球]
set:[足球2, 篮球2, 乒乓球2]
map:{foot=足球3, basket=篮球3, pp3=乒乓球3}
prop:{foot4=足球4, basket4=篮球4, pp4=乒乓球4}
array:足球1,篮球1,乒乓球1,
2)也可以利用之前说的其他方法对其注入,例如构造器注入:
<constructor-arg name="listElement">
<list>
<value>足球</value>
<value>篮球</value>
<value>乒乓球</value>
</list>
</constructor-arg>
以上就是集合和非集合(引用类型、基本类型)的常见注入方式!下面将对其进行补充。
特殊值注入:
1)value与<value>注入方式的区别(及value属性注入与子元素<value>注入)区别:
| 使用子元素<value>注入 | 而使用value属性注入 |
参数值位置 | 写在首尾标签(<value></value>)的中间(不加双引号) | 写在value的属性值中(必须加双引号) |
type属性
| 有(可选) 可以通过type属性指定数据类型 | 无(set注入方式没有) |
参数值包含特殊字符(<, &)时的处理方法 | 两种处理方法。 一、使用<![CDATA[ ]]>标记 二、使用XML预定义的实体引用 | 一种处理方法。即使用XML预定义的实体引用 |
其中,XML预定义的实体引用,如表所示。
实体引用 | 表示的符号 |
< | < |
& | & |
> | > |
子元素<value>注举例 (type+特殊字符):
<property name="name" >
<value type="java.lang.String">z<![CDATA[<]]>s</value> //输出z<s
</property>
<property name="name" value="z<s"></property>//输出z<S
2)String类型注入null或" ".
赋值null:
方法一:<property name="stuName" value="null"> //value属性赋值
</property>
方法二: <property name="stuName">
<value>null</value> //子元素<value>赋值
</property>
方法三: <property name="stuName">
<null/> //利用null标签
</property>
赋值" ":
方法一:<property name="stuName" value=""></property>
方法二:<property name="stuName">
<value></value>
</property>