姝ラ錛?/p>
絎竴姝ワ細寤哄伐紼?br /> File -> New -> Project ->Web Project,"Project Name":MySpringTest,鐒跺悗"Finish";
絎簩姝ワ細瀵煎叆spring鍖?br /> 閫変腑MySpringTest,鍙沖嚮,MyEclipse -> Add Spring Capabilities……,閮介粯璁ゅ嵆鍙紱
絎笁姝ワ細
寤虹珛欏圭洰鎵闇綾伙紱MySpringTest -> src -> New ...錛堜互涓嬩笁涓兘榪欐牱寤猴級Spring 鐨勫紑鍙戞病娉曡嚜鍔ㄧ敓鎴?Bean錛?榪欓噷澶у鍙ソ鎵嬪伐鏉ュ啓浜嗭紝 涔熷緢綆鍗曘?/p>
1銆佹帴鍙ction:錛圡ySpringTest -> src -> New -> interface ,鍙栧悕涓篈ction錛?/p>
public interface Action {
public String execute(String str);
}
2銆佸疄鐜版帴鍙ction鐨勭被UpperAction:錛堝皢鍏?message 灞炴т笌杈撳叆瀛楃涓茬浉榪炴帴錛屽茍榪斿洖鍏跺ぇ鍐欏艦寮忋傦級
錛圡ySpringTest -> src -> New -> class ,鍙栧悕涓篣pperAction錛?
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();
}
}
3銆佸疄鐜版帴鍙ction鐨勭被LowerAction:
錛堝皢鍏?message 灞炴т笌杈撳叆瀛楃涓茬浉榪炴帴錛屽茍榪斿洖鍏跺皬鍐欏艦寮忋傦級
錛圡ySpringTest -> src -> New -> class ,鍙栧悕涓篖owerAction錛?nbsp;
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();
}
}
4銆佸仛嫻嬭瘯鐢ㄧ殑SimpleTest綾伙細
錛圡ySpringTest -> src -> New -> class ,鍙栧悕涓篠impleTest錛?
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"));
}
}
5銆侀厤緗產pplicationContext.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涓殑縐佹湁鎴愬憳鍚?br /> -->
<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>
5銆佸湪WEB-INF/class 鐩綍涓嬪緩绔嬩竴涓猯og4j.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
]]>