Student类
package com.spring;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@AllArgsConstructor
@NoArgsConstructor
@Data
public class Student {
public String name;
public int age;
}
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="stu1" class="com.spring.Student">bean>
beans>
通过set方法赋值
xml文件
<bean id="stu1" class="com.spring.Student">
<property name="name" value="张三">property>
<property name="age" value="18">property>
bean>
通过有参构造器赋值
xml文件
<bean id="stu1" class="com.spring.Student">
<constructor-arg value="张三" name="name">constructor-arg>
<constructor-arg value="24">constructor-arg>
bean>
赋值NULL
<bean id="stu1" class="com.spring.Student">
<constructor-arg name="name">
<null />
constructor-arg>
<constructor-arg value="24">constructor-arg>
bean>
xml实体
如 < > 有特殊含义而不能直接使用
<bean id="stu1" class="com.spring.Student">
<constructor-arg value="<张三>" name="name">constructor-arg>
<constructor-arg value="24">constructor-arg>
bean>
CDATA
里面的内容将被原样解析CDATA不能直接写到属性中,可以写到子标签里
<bean id="stu1" class="com.spring.Student">
<constructor-arg name="name">
<value>
]]>
value>
constructor-arg>
<constructor-arg value="24">constructor-arg>
bean>
Clazz类
package com.spring;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Clazz {
private String clazzName;
private int num;
}
Student类
package com.spring;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@AllArgsConstructor
@NoArgsConstructor
@Data
public class Student {
public String name;
public int age;
private Clazz clazz;
}
ref:引用IOC容器中某个Bean的id
<bean id="clz1" class="com.spring.Clazz">
<property name="clazzName" value="clz1">property>
<property name="num" value="1">property>
bean>
<bean id="stu1" class="com.spring.Student">
<property name="age" value="18">property>
<property name="name" value="张三">property>
<property name="clazz" ref="clz1">property>
bean>
必须先实例化
<bean id="stu1" class="com.spring.Student">
<property name="age" value="18">property>
<property name="name" value="张三">property>
<property name="clazz" ref="clz1">property>
<property name="clazz.clazzName" value="clz11">property>
<property name="clazz.num" value="11">property>
bean>
不能通过IOC容器直接获取
<bean id="stu1" class="com.spring.Student">
<property name="age" value="18">property>
<property name="name" value="张三">property>
<property name="clazz">
<bean id="clazz1" class="com.spring.Clazz">
<property name="clazzName" value="clazz1">property>
<property name="num" value="1">property>
bean>
property>
bean>
Stu类
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;
public void setSets(Set<String> sets) {
this.sets = sets;
}
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;
}
}
<property name="courses">
<array>
<value>java课程value>
<value>数据库课程value>
array>
property>
<property name="list">
<list>
<value>张三value>
<value>小三value>
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>
<bean id="course1" class="com.atguigu.spring5.collectiontype.Course">
<property name="cname" value="Spring5 框架">property>
bean>
<bean id="course2" class="com.atguigu.spring5.collectiontype.Course">
<property name="cname" value="MyBatis 框架">property>
bean>
<property name="courseList">
<list>
<ref bean="course1">ref>
<ref bean="course2">ref>
list>
property>
(1)在 spring 配置文件中引入名称空间 util
(2)使用 util 标签完成集合注入提取
<util:list id="bookList">
<value>book1value>
<value>book2value>
<value>book3value>
util:list>
<bean id="book" class="com.atguigu.spring5.collectiontype.Book">
<property name="list" ref="bookList">property>
bean>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd" >
<bean id="clz1" class="com.spring.Clazz">
<property name="clazzName" value="clz1">property>
<property name="num" value="1">property>
bean>
<bean id="stu1" class="com.spring.Student" p:age="18" p:name="张三" p:clazz-ref="clz1"> bean>
beans>