步驟: java
第一步:建工程
File -> New -> Project ->Web Project,"Project Name":MySpringTest,而後"Finish"; spring
第二步:導入spring包
選中MySpringTest,右擊,MyEclipse -> Add Spring Capabilities……,都默認便可; apache
第三步:
創建項目所需類;MySpringTest -> src -> New ...(如下三個都這樣建)Spring 的開發無法自動生成 Bean, 這裏你們只好手工來寫了, 也很簡單。 app
一、接口Action:(MySpringTest -> src -> New -> interface ,取名爲Action) 測試
public
interface
Action {
public
String execute(String str);
}
二、實現接口Action的類UpperAction:(將其 message 屬性與輸入字符串相鏈接,並返回其大寫形式。)
(MySpringTest -> src -> New -> class ,取名爲UpperAction) ui
public
class
UpperAction
implements
Action {
private
String message;
public
String getMessage() {
return
message;
}
public
void
setMessage(String message) {
this
.message
=
message;
}
public
String execute(String str) {
return
(getMessage()
+
str).toUpperCase();
}
}
三、實現接口Action的類LowerAction: this
(將其 message 屬性與輸入字符串相鏈接,並返回其小寫形式。)
(MySpringTest -> src -> New -> class ,取名爲LowerAction) spa
public
class
LowerAction
implements
Action {
private
String message;
public
String getMessage() {
return
message;
}
public
void
setMessage(String message) {
this
.message
=
message;
}
public
String execute(String str) {
return
(getMessage()
+
str).toLowerCase();
}
}
四、作測試用的SimpleTest類:
(MySpringTest -> src -> New -> class ,取名爲SimpleTest)
import
org.springframework.context.ApplicationContext;
import
org.springframework.context.support.FileSystemXmlApplicationContext;
public
class
SimpleTest {
public
static
void
main(String args[]) {
SimpleTest test
=
new
SimpleTest();
test.testQuickStart();
}
public
void
testQuickStart() {
ApplicationContext ctx
=
new
FileSystemXmlApplicationContext(
"
src/applicationContext.xml
"
);
Action action
=
(Action) ctx.getBean(
"
action1
"
);
System.out.println(action.execute(
"
Rod Johnson
"
));
action
=
(Action) ctx.getBean(
"
action2
"
);
System.out.println(action.execute(
"
jeckj
"
));
}
}
五、配置applicationContext.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"
xsi:schemaLocation
="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"
>
<
description
>
Spring Quick Start
</
description
>
<!--
該處bean中的name值必須是 其對應的class中的私有成員名
-->
<
bean
id
="action1"
class
="UpperAction"
>
<
property
name
="message"
>
<
value
>
HeLLo
</
value
>
</
property
>
</
bean
>
<
bean
id
="action2"
class
="LowerAction"
>
<
property
name
="message"
>
<
value
>
HeLLo
</
value
>
</
property
>
</
bean
>
</
beans
>
五、在WEB-INF/class 目錄下創建一個log4j.propertie
log4j.rootLogger
=
ERROR
,
stdout
log4j.appender.stdout
=
org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout
=
org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern
=
%d %5p (%F:%L) - %m%n
第四步:調試
雙擊 Package Explorer 下 MySpringTest/src/TestAction.java 打開源代碼,點擊菜單 Run -> Run As -> 1. Java Application, 若是沒有錯誤的話將會出現以下
HELLOROD JOHNSON
hellojeckj