https://blog.csdn.net/weixin_52003758/article/details/124214494
3、XML注入其他类型属性
1、字面量
(1)NULL值
(2)属性值包含特殊符号
a.把<>进行转义<, >
b.把带特殊符号内容写到CDATA里
>]]>
2、注入属性-外部bean
(1)创建两个类service类和dao类
(2)在service调用dao里面的方法
3、注入属性-内部bean
4、注入集合类属性
// Course.java
package com.company;
public class Course {
private String cname;
public void setCname(String cname){
this.cname = cname;
}
}
//Stu.java
package com.company;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Stu {
// 1、数组类型属性
private String[] courses;
// 2、list集合类型属性
private List<String> list;
// 3、map集合类型属性
private Map<String, String> maps;
// 4、set集合类型属性
private Set<String> sets;
// 5、自定义类属性
private List<Course> l_courses;
public void setCourses(String[] courses){
this.courses = courses;
}
public void setList(List<String> list){
this.list = list;
}
public void setMaps(Map<String, String> maps){
this.maps = maps;
}
public void setSets(Set<String> sets){
this.sets = sets;
}
public void setL_courses(List<Course> l_courses) {
this.l_courses = l_courses;
}
}
// bean1.xml
<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="stu" class="com.company.Stu">
<property name="courses">
<list>
<value>java课程value>
<value>数据库课程value>
list>
property>
<property name="list">
<list>
<value>gujiavalue>
<value>liujialevalue>
list>
property>
<property name="maps">
<map>
<entry key="Java" value="java">entry>
<entry key="PHP" value="php">entry>
map>
property>
<property name="sets">
<set>
<value>Mysqlvalue>
<value>Redisvalue>
set>
property>
<property name="l_courses">
<list>
<ref bean="course1">ref>
<ref bean="course2">ref>
list>
property>
bean>
<bean id="course1" class="com.company.Course">
<property name="cname" value="Spring">property>
bean>
<bean id="course2" class="com.company.Course">
<property name="cname" value="Vue">property>
bean>
beans>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)