Spring中采用set方式注入不同类型的数据

基本类型和包装类型:直接使用 value属性 赋值
/**
 * Student类
 */
@Getter
@Setter
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class Student {
	/**
	 * 主键id
	 */
	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">
	       
	<!-- Student类的配置 -->
	<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
/**
 * Student类
 */
@Getter
@Setter
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class Student {
	/**
	 * 主键id
	 */
	private Integer id;
	/**
	 * 姓名
	 */
	private String name;
	/**
	 * 分配的老师
	 */
	private Teacher teacher;
}
/**
 * Teacher类
 */
@Getter
@Setter
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class Teacher {
	/**
	 * 主键id
	 */
	private Integer id;
	/**
	 * 姓名
	 */
	private String name;
}
<!-- 使用内部bean的方式 -->
<?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的property属性中使用bean标签配置内部bean -->
	    <property name="teacher">
	        <bean class="com.xjhwang.spring.di.Teacher">
	            <property name="id" value="10001"/>
	            <property name="name" value="Java"/>
	        </bean>
	    </property>
	</bean>
</beans>
<!-- 使用ref指向同级bean的方式 -->
<?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"/>
	    <!-- 指向id为teacher的bean -->
	    <property name="teacher" ref="teacher"/>
	</bean>
	
	<!-- 配置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标签
/**
 * Student类
 */
@Getter
@Setter
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class Student {
	/**
	 * 主键id
	 */
	private Integer id;
	/**
	 * 姓名
	 */
	private String name;
	/**
	 * 分配的老师
	 */
	private Teacher teacher;
	/**
	 * 课程
	 */
	private List<Course> courses;
}
/**
 * Course类
 */
@Getter
@Setter
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class Course {
	/**
	 * 主键id
	 */
	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标签 -->
	        <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>
	    <!-- 使用 util:list 标签 -->
	    <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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值