原创: applicationContent.xml配置及bean的的属性值为:基本类、集合List类型、map类型
前言:
需要三个文件为配置文件
a) StudentDomain.java类型
b)beans.xml 其实就是applicationContent.xml更换名字
c) 测试类
a) StudentDomain.java类型
package org.ibaiqi.inforjob.service; import java.util.List; import java.util.Map; /** * @program: job_systerm_parent * @description: 学生实体类 * @author: zhangdaxu * @create: 2020-03-02 11:28 */ public class StudentDomain { Integer sid; String stuName; String sex; int age; //每门成绩列表 List<Double> scores; Map<String,String> teacherMap; public Map<String, String> getTeacherMap() { return teacherMap; } public void setTeacherMap(Map<String, String> teacherMap) { this.teacherMap = teacherMap; } public List<Double> getScores() { return scores; } public void setScores(List<Double> scores) { this.scores = scores; } public Integer getSid() { return sid; } public void setSid(Integer sid) { this.sid = sid; } public String getStuName() { return stuName; } public void setStuName(String stuName) { this.stuName = stuName; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "StudentDomain{" + "sid=" + sid + ", stuName='" + stuName + '\'' + ", sex='" + sex + '\'' + ", age=" + age + ", scores=" + scores + ", teacherMap=" + teacherMap + '}'; } }
b)beans.xml 其实就是applicationContent.xml更换名字
<?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <beans> <bean id="stu2" class="org.ibaiqi.inforjob.service.StudentDomain"> <!-- <property>:属性 其实依赖注入,注入就是传值,通过<property>标签可以实现传值 name:属性名 value:属性值 --> <!--1:基本数据类型--> <property name="sid" value="1001"></property> <property name="stuName" value="张三同学"></property> <property name="sex" value="男"></property> <!--属性赋值另一种方式嵌套方式--> <property name="age"> <value>15</value> </property> <!---2: 对List集合进行注入--> <property name="scores"> <!--List 集合类型--> <list> <value>90</value> <value>96</value> <value>90</value> </list> </property> <property name="teacherMap"> <!--3:map数据类型--> <map> <entry> <key> <value>10001</value> </key> <value>张老师</value> </entry> <entry> <key> <value>10002</value> </key> <value>李老师</value> </entry> </map> </property> </bean> <!--4: 引用数据类型,如学生可能有多们老师,需要加老师类,在此跳过 引用类型用 <property name="teacher" ref="引用类型的bean的Id"></property> --> </beans> </beans>
c) 测试类
package org.ibaiqi.inforjob.service.imp; import org.ibaiqi.inforjob.service.StudentDomain; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * @program: job_systerm_parent * @description: 测试 * @author: zhangdaxu * @create: 2020-03-02 11:32 */ public class StudentDomainTest { @Test public void stuTest() { ApplicationContext ac= new ClassPathXmlApplicationContext("beans.xml"); System.out.println("开始测试学生配置applicationContent"); StudentDomain stu2 = ac.getBean("stu2", StudentDomain.class); System.out.println(stu2); } }
做产品的程序,才是好的程序员!