J2EE实验报告7Spring的IOCWord格式文档下载.docx
《J2EE实验报告7Spring的IOCWord格式文档下载.docx》由会员分享,可在线阅读,更多相关《J2EE实验报告7Spring的IOCWord格式文档下载.docx(11页珍藏版)》请在冰豆网上搜索。
签字:
日期:
成绩
实验内容
1功能描述
2实验步骤
2.1搭建Spring框架
(0)新建webproject项目,项目名称自定义(我取名为EX_Ioc);
(1)将spring需要的jar包拷贝到______下;
(2)在src下新建applicationContext.xml文件
<
?
xmlversion="
1.0"
encoding="
UTF-8"
>
beans
xmlns="
http:
//www.springframework.org/schema/beans"
xmlns:
xsi="
//www.w3.org/2001/XMLSchema-instance"
p="
//www.springframework.org/schema/p"
aop="
//www.springframework.org/schema/aop"
xsi:
schemaLocation="
//www.springframework.org/schema/beans
http:
//www.springframework.org/schema/beans/spring-beans-3.0.xsd
//www.springframework.org/schema/aop
//www.springframework.org/schema/aop/spring-aop-3.0.xsd"
<
/beans>
2.2注入普通类及属性
(1)在src下新增包cn.edu.xmut.first,在该包下新建类Person.java:
Person.java
packagecn.edu.xmut.first;
publicclassPerson{
privateStringname;
publicvoidsayHello(){
System.out.println("
hello,iam"
+name);
}
//省略getset方法
}
(2)在applicationContext.xml文件中添加配置
!
--frist配置简单类及属性-->
beanid="
person"
class="
____"
<
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.java
IPerson.java
packagecn.edu.xmut.second;
publicinterfaceIPerson{
publicvoiduserAxe();
IAxe.java
publicinterfaceIAxe{
publicvoidchop();
Chinese.java
publicclassChineseimplementsIPerson{
privateIAxeaxe;
publicvoiduseAxe(){
axe.chop();
American.java
publicclassAmericanimplementsIPerson{
SteelAxe.java
publicclassSteelAxeimplementsIAxe{
publicvoidchop(){
steelAxeisquick"
StoneAxe.java
publicclassStoneAxeimplementsIAxe{
stoneAxeisslow"
--second配置类及使用bean-->
stoneAxe"
________"
steelAxe"
chinese"
axe"
ref="
american"
修改类MyTest.java:
//second配置类及使用bean
Chinesechinese=(Chinese)apc.getBean("
chinese.useAxe();
Americanamerican=(American)apc.getBean("
american.useAxe();
2.3配置类及使用bean--构造注入
(1)在包cn.edu.xmut.second下,新建类France.java
France.java
publicclassFranceimplementsIPerson{
privateIAxeaxe2;
publicFrance(){
publicFrance(IAxeaxe,IAxeaxe2,Stringname){
this.axe=axe;
this.axe2=axe2;
this.name=name;
axe2.chop();
System.out.println(name);
france"
constructor-argref="
/constructor-arg>
constructor-argvalue="
//构造注入
Francefrance=(France)apc.getBean("
_____"
france.useAxe();
2.4静态工厂注入
(1)在src下新增包cn.edu.xmut.third,在该包下新建类Dog.java、Cat.java、Factory.java和接口IBeing.java
IBeing.java
packagecn.edu.xmut.third;
publicinterfaceIBeing{
publicvoid___________;
Dog.java
publicinterfaceDogimplementsIBeing{
Hello,iamdog!
Mynameis:
"
+name);
Cat.java
publicinterfaceCatimplementsIBeing{
Hello,iamcat!
Factory.java
publicclassFactory{
publicstaticIBeinggetBeing(Stringtype){
if(type.equalsIgnoreCase("
)){
returnnewDog();
}else{
returnnewCat();
}
}
--静态工厂生成bean-->
dog"
factory-method="
getBeing"
name"
cat"
//静态工厂生成bean
Dogdog=(Dog)apc.getBean("
dog.sayHello();
Catcat=(Cat)apc.getBean("
cat.sayHello();
2.5实例工厂注入
(1)在src下新增包cn.edu.xmut.fourth,在该包下新建类Chinese.java、American.java、Factory.java和接口IPerson.java
packagecn.edu.xmut.fourth;
publicvoid______________();
publicinterfaceChineseimplementsIPerson{
Hello,iamchinese!
publicinterfaceAmericanimplementsIPerson{
Hello,iamamerican!
packagecn.edu.xmut.fourth;
publicIPersongetPerson(Stringtype){
returnnewChinese();
returnnew________;
--实例工厂构造bean-->
f"
chn"
factory-bean="
___"
getPerson"
ame"
__"
//实例工厂生成bean
Chinesechn=___________;
chn.sayHello();
Americaname=___________;
ame.sayHello();
3心得体会
不得放空