2024年6月3日发(作者:)

J2EE实验报告

学号

姓名

教师

班级

上课时间 上课地点

实验八 Spring 的IOC

1. 实验目的

1.1、掌握Spring框架的建立步骤;

1.2、掌握Spring的Ioc注入方式:传值注入和构造注入;

1.3、掌握Spring的静态工厂注入方式;

1.4、掌握Spring的实例工厂注入方式。

2. 实验环境

2.1 MyEclipse

2.2 Window XP

2.3 MySQL

4. 教师批改意见

签字:

日期:

成绩

实验内容

1功能描述

1.1、掌握Spring框架的建立步骤;

1.2、掌握Spring的Ioc注入方式:传值注入和构造注入;

1.3、掌握Spring的静态工厂注入方式;

1.4、掌握Spring的实例工厂注入方式。

2实验步骤

2.1 搭建Spring 框架

(0) 新建web project项目,项目名称自定义(我取名为EX_Ioc);

(1) 将spring需要的jar包拷贝到______下;

(2)在src下新建文件

xmlns="/schema/beans"

xmlns:xsi="/2001/XMLSchema-instance"

xmlns:p="/schema/p"

xmlns:aop="/schema/aop"

xsi:schemaLocation="/schema/bean

s

/schema/beans/spring-beans-3.

/schema/aop

/schema/aop/

d">

2.2 注入普通类及属性

(1)在src下新增包,在该包下新建类:

package ;

public class Person {

private String name;

public void sayHello(){

n("hello, i am " + name);

}

//省略get set 方法

}

(2)在文件中添加配置

(3)编写测试代码

在src下新增包,在该包下新建类:

package ;

import ationContext;

import

athXmlApplicationCon

text;

import ;

public class MyTest{

public static void main(String[] args) {

ApplicationContext apc = new ______("______.xml");

Person person = (______) n("______");

lo();

}

}

(4)运行测试代码,查看结果。

2.3 配置类及使用bean--传值注入

(1)在src下新增包,在该包下新建接口、

和类、、、

package ;

public interface IPerson {

public void userAxe();

}

package ;

public interface IAxe {

public void chop();

}

package ;

public class Chinese implements IPerson {

private IAxe axe;

public void useAxe() {

();

}

}

package ;

public class

American

implements IPerson {

private IAxe axe;

public void useAxe() {

();

}

}

package ;

public class SteelAxe implements IAxe {

public void chop() {

n("steelAxe is quick");

}

}

package ;

public class StoneAxe implements IAxe {

public void chop() {

n("stoneAxe is slow");

}

}

(2)在文件中添加配置

(3)编写测试代码

修改类:

package ;

import ationContext;

import

athXmlApplicationCon

text;

public class MyTest{

public static void main(String[] args) {

ApplicationContext apc = new ______("______.xml");

//second 配置类及使用bean

Chinese chinese = (Chinese) n("____");

();

American american = (American) n("____");

();

}

}

(4)运行测试代码,查看结果。

2.3 配置类及使用bean--构造注入

(1)在包下,新建类

package ;

public class France implements IPerson {

private IAxe axe;

private IAxe axe2;

private String name;

public France() {

}

public France(IAxe axe,IAxe axe2,String name) {

= axe;

2 = axe2;

= name;

}

public void useAxe() {

();

();

n(name);

}

}

(2)在文件中添加配置

(3)编写测试代码

修改类:

package ;

import ationContext;

import

athXmlApplicationCon

text;

public class MyTest{

public static void main(String[] args) {

ApplicationContext apc = new ______("______.xml");

//构造注入

France france = (France) n("_____");

();

}

}

(4)运行测试代码,查看结果。

2.4 静态工厂注入

(1)在src下新增包,在该包下新建类、、

和接口

package ;

public interface IBeing {

public void ___________;

}

package ;

public interface Dog implements IBeing {

private String name;

public void sayHello(){

n("Hello,i am dog!My name is:"+name);

}

}

package ;

public interface Cat implements IBeing {

}

private String name;

public void sayHello(){

n("Hello,i am cat!My name is:"+name);

}

package ;

public class

Factory

{

public static IBeing getBeing(String type){

if(IgnoreCase("____")){

return new Dog();

}else {

return new Cat();

}

}

}

(2)在文件中添加配置

(3)编写测试代码

修改类:

package ;

import ationContext;

import

athXmlApplicationCon

text;

public class MyTest{

public static void main(String[] args) {

ApplicationContext apc = new ______("______.xml");

}

}

//静态工厂生成bean

Dog dog = (Dog) n("____");

lo();

Cat cat = (Cat) n("____");

lo();

(4)运行测试代码,查看结果。

2.5 实例工厂注入

(1)在src下新增包,在该包下新建类、

、和接口

package . fourth;

public interface IPerson {

public void ______________();

}

package . fourth;

public interface

Chinese

implements IPerson {

private String name;

public void sayHello(){

n("Hello,i am chinese!My name is:"+name);

}

}

package . fourth;

public interface

American

implements IPerson {

private String name;

public void sayHello(){

n("Hello,i am

american

!My name is:"+name);

}

}

package ;

public class

Factory

{

public IPerson getPerson (String type){

if(IgnoreCase("____")){

return new Chinese();

}else {

return new ________;

}

}

}

(2)在文件中添加配置

(3)编写测试代码

修改类:

package ;

import ationContext;

import

athXmlApplicationCon

text;

public class MyTest{

public static void main(String[] args) {

ApplicationContext apc = new ______("______.xml");

//实例工厂生成bean

Chinese chn = ___________;

lo();

American ame = ___________;

lo();

}

}

(4)运行测试代码,查看结果。

3心得体会

不得放空