如果您无法下载资料,请参考说明:
1、部分资料下载需要金币,请确保您的账户上有足够的金币
2、已购买过的文档,再次下载不重复扣费
3、资料包下载后请先用软件解压,在使用对应软件打开
Spring配置文件实例01(实例:-学生-书包-书籍-)建好一个测试java工程简单描述第一个简单案例:通过spring配置(属性注入,构造函数的注入)来生成多个学生。构造一个studentbean(三个属性)privateStringstudentname;privateStringorigin;privateintage为其构造set(),get()方法。为其生成构造函数在spring的配置文件中对其进行属性注入或构造函数注入<!--学生的bean属性注入--><beanid="student1"class="com.workit.bean.StudentBean"><propertyname="studentname"value="jack"></property><propertyname="origin"value="eng"></property><propertyname="age"value="23"></property></bean><!--student2的构造函数--><beanid="student2"class="com.workit.bean.StudentBean"><constructor-argname="origin"type="String"value="usa"></constructor-arg><constructor-argname="age"type="int"value="23"></constructor-arg></bean>在配置文件生成好后,方可进行简单的测试编写简单的测试类:publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubApplicationContextac=newClassPathXmlApplicationContext("applicationContext.xml");StudentBeanstudent1=(StudentBean)ac.getBean("student1");for(inti=0;i<student1.getPb().getListbook().size();i++){System.out.println(student1.getStudentname()+"\t"+student1.getOrigin()+"\t"+student1.getAge());}System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");StudentBeanstudent2=(StudentBean)ac.getBean("student2");for(inti=0;i<student2.getPb().getListbook().size();i++){System.out.println(student2.getStudentname()+"\t"+student2.getOrigin()+"\t"+student2.getAge());}在生成学生后,完成相关扩充:在学生类添加属性书包,一个学生(多属性)可拥有多个书包,书包里面可装多本书籍…与上述方法雷同,重新编写studentbeanprivateStringstudentname;privateStringorigin;privateintage;privatePackgebookBeanpb;privatePropertiesproperties;构建书包类:PackgebookBeanprivateStringpackgename;privateintweight;List<BookBean>listbook;privateMap<String,String>pbmap;privateList<String>bookstr;privateList<BookBean>listotherbook;privateMap<String,BookBean>bookmap;privateSet<String>listbstr;privateSet<BookBean>setbookbean;构建书籍类:BookBeanprivateStringb