基本类型和包装类型:直接使用 value属性
赋值
@Getter
@Setter
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class Student {
private Integer id;
private String name;
}
<?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="student" class="com.xjhwang.spring.di.Student">
<property name="id" value="1001"/>
<property name="name" value="real-xjhwang"/>
</bean>
</beans>
对象类型:使用 bean标签
配置内部bean,或者使用 ref属性
指向同级的其他bean
@Getter
@Setter
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class Student {
private Integer id;
private String name;
private Teacher teacher;
}
@Getter
@Setter
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class Teacher {
private Integer id;
private String name;
}
<?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="student" class="com.xjhwang.spring.di.Student">
<property name="id" value="1001"/>
<property name="name" value="real-xjhwang"/>
<property name="teacher">
<bean class="com.xjhwang.spring.di.Teacher">
<property name="id" value="10001"/>
<property name="name" value="Java"/>
</bean>
</property>
</bean>
</beans>
<?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="student" class="com.xjhwang.spring.di.Student">
<property name="id" value="1001"/>
<property name="name" value="real-xjhwang"/>
<property name="teacher" ref="teacher"/>
</bean>
<bean id="teacher" class="com.xjhwang.spring.di.Teacher">
<property name="id" value="10001"/>
<property name="name" value="Java"/>
</bean>
</beans>
集合类型:使用 集合标签
或者引入 util命名空间
后使用 util:集合标签
(例如:使用 list标签
或者引入 util命名空间
后使用 util:list标签
)
@Getter
@Setter
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class Student {
private Integer id;
private String name;
private Teacher teacher;
private List<Course> courses;
}
@Getter
@Setter
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class Course {
private Integer id;
private String name;
}
<?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="student" class="com.xjhwang.spring.di.Student">
<property name="id" value="1001"/>
<property name="name" value="real-xjhwang"/>
<property name="teacher">
<bean class="com.xjhwang.spring.di.Teacher">
<property name="id" value="10001"/>
<property name="name" value="Java"/>
</bean>
</property>
<property name="courses">
<list>
<bean class="com.xjhwang.spring.di.Course">
<property name="id" value="101"/>
<property name="name" value="Java"/>
</bean>
<bean class="com.xjhwang.spring.di.Course">
<property name="id" value="102"/>
<property name="name" value="数据结构"/>
</bean>
<bean class="com.xjhwang.spring.di.Course">
<property name="id" value="103"/>
<property name="name" value="算法"/>
</bean>
</list>
</property>
</bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">
<bean id="student" class="com.xjhwang.spring.di.Student">
<property name="id" value="1001"/>
<property name="name" value="real-xjhwang"/>
<property name="teacher">
<bean class="com.xjhwang.spring.di.Teacher">
<property name="id" value="10001"/>
<property name="name" value="Java"/>
<property name="courses" ref="courses"/>
</bean>
</property>
<property name="courses" ref="courses"/>
</bean>
<util:list id="courses">
<bean class="com.xjhwang.spring.di.Course">
<property name="id" value="101"/>
<property name="name" value="Java"/>
</bean>
<bean class="com.xjhwang.spring.di.Course">
<property name="id" value="102"/>
<property name="name" value="数据结构"/>
</bean>
<bean class="com.xjhwang.spring.di.Course">
<property name="id" value="103"/>
<property name="name" value="算法"/>
</bean>
</util:list>
</beans>