J2EE实验报告学号姓名班级上课地点教师上课时间实验八Spring的IOC1.实验目的1.1、掌握Spring框架的建立步骤;1.2、掌握Spring的Ioc注入方式:传值注入和构造注入;1.3、掌握Spring的静态工厂注入方式;1.4、掌握Spring的实例工厂注入方式。2.实验环境2.1MyEclipse2.2WindowXP2.3MySQL4.教师批改意见签字:日期:成绩实验内容1功能描述1.1、掌握Spring框架的建立步骤;1.2、掌握Spring的Ioc注入方式:传值注入和构造注入;1.3、掌握Spring的静态工厂注入方式;1.4、掌握Spring的实例工厂注入方式。2实验步骤2.1搭建Spring框架(0)新建webproject项目,项目名称自定义(我取名为EX_Ioc);(1)将spring需要的jar包拷贝到______下;(2)在src下新建applicationContext.xml文件?xmlversion=1.0encoding=UTF-8?beansxmlns=:xsi=:p=:aop=:schemaLocation=://://://注入普通类及属性(1)在src下新增包cn.edu.xmut.first,在该包下新建类Person.java:Person.javapackagecn.edu.xmut.first;publicclassPerson{privateStringname;publicvoidsayHello(){System.out.println(hello,iam+name);}//省略getset方法}(2)在applicationContext.xml文件中添加配置!--frist配置简单类及属性--beanid=personclass=____propertyname=____value=xmut/property/bean(3)编写测试代码在src下新增包cn.edu.xmut.test,在该包下新建类MyTest.java:packagecn.edu.xmut.test;importorg.springframework.context.ApplicationContext;importorg.springframework.context.support.ClassPathXmlApplicationContext;importcn.edu.xmut.HelloSpring.app.frist.Person;publicclassMyTest{publicstaticvoidmain(String[]args){ApplicationContextapc=new______(______.xml);Personperson=(______)apc.getBean(______);person.sayHello();}}(4)运行测试代码,查看结果。2.3配置类及使用bean--传值注入(1)在src下新增包cn.edu.xmut.second,在该包下新建接口IPerson.java、IAxe.java和类Chinese.java、American.java、SteelAxe.java、Stone.javaIPerson.javapackagecn.edu.xmut.second;publicinterfaceIPerson{publicvoiduserAxe();}IAxe.javapackagecn.edu.xmut.second;publicinterfaceIAxe{publicvoidchop();}Chinese.javapackagecn.edu.xmut.second;publicclassChineseimplementsIPerson{privateIAxeaxe;publicvoiduseAxe(){axe.chop();}}American.javapackagecn.edu.xmut.second;publicclassAmericanimplementsIPerson{privateIAxeaxe;publicvoiduseAxe(){axe.chop();}}SteelAxe.javapackagecn.edu.xmut.second;publicclassSteelAxeimplementsIAxe{publicvoidchop(){System.out.println(steelAxeisquick!!!);}}StoneAxe.javapackagecn.edu.xmut.second;publicclassStoneAxeimplementsIAxe{publicvoidchop(){System.out.println(stoneAxeisslow!!!);}}(2)在applicationContext.xml文件中添加配置!--second配置类及使用bean--beanid=stoneAxeclass=________/beanbeanid=steelAxeclass=________/beanbeanid=chineseclass=________propertyname=axeref=________/property/beanbeanid=americanclass=________propertyname=axeref=stoneAxe/property/bean(3)编写测试代码修改类MyTest.java:packagecn.edu.xmut.test;importorg.springframework.context.ApplicationContext;importorg.springframework.context.support.ClassPathXmlApplicationContext;publicclassMyTest{publicstaticvoidmain(String[]args){ApplicationContextapc=new______(______.xml);//second配置类及使用beanChinesechinese=(Chinese)apc.getBean(____);chinese.useAxe();Americanamerican=(American)apc.getBean(____);american.useAxe();}}(4)运行测试代码,查看结果。2.3配置类及使用bean--构造注入(1)在包cn.edu.xmut.second下,新建类France.javaFrance.javapackagecn.edu.xmut.second;publicclassFranceimplementsIPerson{privateIAxeaxe;privateIAxeaxe2;privateStringname;publicFrance(){}publicFrance(IAxeaxe,IAxeaxe2,Stringname){this.axe=axe;this.axe2=axe2;this.name=name;}publicvoiduseAxe(){axe.chop();axe2.chop();System.out.println(name);}}(2)在applicationContext.xml文件中添加配置beanid=franceclass=______constructor-argref=____/constructor-argconstructor-argref=____/constructor-argconstructor-argvalue=____/constructor-arg/bean(3)编写测试代码修改类MyTest.java:packagecn.edu.xmut.test;importorg.springframework.context.ApplicationContext;importorg.springframework.context.support.ClassPathXmlApplicationContext;publicclassMyTest{publicstaticvoidmain(String[]args){ApplicationContextapc=new______(______.xml);//构造注入Francefrance=(France)apc.getBean(_____);france.useAxe();}}(4)运行测试代码,查看结果。2.4静态工厂注入(1)在src下新增包cn.edu.xmut.third,在该包下新建类Dog.java、Cat.java、Factory.java和接口IBeing.javaIBeing.javapackagecn.edu.xmut.third;publicinterfaceIBeing{publicvoid___________;}Dog.javapackagecn.edu.xmut.third;publicinterfaceDogimplementsIBeing{privateStringname;publicvoidsayHello(){System.out.println(Hello,iamdog!Mynameis:+name);}}Cat.javapackagecn.edu.xmut.third;publicinterfaceCatimplementsIBeing{privateStringname;publicvoidsayHello(){System.out.println(Hello,iamcat!Mynameis:+name);}}Factory.javapackagecn.edu.xmut.third;publicclassFactory{publicstaticIBeinggetBeing(Stringtype){if(type.equalsIgnoreCase(____)){returnnewDog();}else{returnnewCat();}}}(2)在applicationContext.xml文件中添加配置!--静态工厂生成bean--beanid=dogclass=____factory-method=getBeingconstructor-argvalue=dog/constructor-argpropertyname=namevalue=____/property/beanbeanid=catclass=____factory-method=getBeingconstructor-argvalue=cat/constructor-argpropertyname=namevalue=____/pro