隨筆 - 147  文章 - 71  trackbacks - 0
          <2013年3月>
          242526272812
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          常用鏈接

          留言簿(1)

          隨筆分類(lèi)(146)

          隨筆檔案(147)

          文章分類(lèi)(28)

          文章檔案(28)

          喜歡的Blog

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          步驟:

          第一步:建工程
             File -> New -> Project ->Web Project,"Project Name":MySpringTest,然后"Finish";

          第二步:導(dǎo)入spring包
             選中MySpringTest,右擊,MyEclipse -> Add Spring Capabilities……,都默認(rèn)即可;

          第三步:
             建立項(xiàng)目所需類(lèi);MySpringTest -> src -> New ...(以下三個(gè)都這樣建)Spring 的開(kāi)發(fā)沒(méi)法自動(dòng)生成 Bean, 這里大家只好手工來(lái)寫(xiě)了, 也很簡(jiǎn)單。

          1、接口Action:(MySpringTest -> src -> New -> interface ,取名為Action)

          public interface Action {
              
          public String execute(String str);
          }

          2、實(shí)現(xiàn)接口Action的類(lèi)UpperAction:(將其 message 屬性與輸入字符串相連接,并返回其大寫(xiě)形式。)
             (MySpringTest -> src -> New -> class ,取名為UpperAction)

          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、實(shí)現(xiàn)接口Action的類(lèi)LowerAction:

          (將其 message 屬性與輸入字符串相連接,并返回其小寫(xiě)形式。)
             (MySpringTest -> src -> New -> class ,取名為L(zhǎng)owerAction) 

          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、做測(cè)試用的SimpleTest類(lèi):
          (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"));
              }
          }
          5、配置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值必須是 其對(duì)應(yīng)的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>
          5、在WEB-INF/class 目錄下建立一個(gè)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 
          第四步:調(diào)試
          雙擊 Package Explorer 下 MySpringTest/src/TestAction.java 打開(kāi)源代碼,點(diǎn)擊菜單 Run -> Run As -> 1. Java Application, 如果沒(méi)有錯(cuò)誤的話(huà)將會(huì)出現(xiàn)如下
          HELLOROD JOHNSON
          hellojeckj
          posted on 2013-03-27 20:18 飛翔天使 閱讀(346) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): Spring

          只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 邵阳市| 全椒县| 阳城县| 汾阳市| 卢氏县| 太原市| 娄底市| 金门县| 靖西县| 大新县| 拉萨市| 秦安县| 平谷区| 芮城县| 永康市| 盐边县| 仲巴县| 阳山县| 临邑县| 武汉市| 天长市| 鹿泉市| 当阳市| 任丘市| 贵德县| 蕲春县| 稷山县| 彭州市| 太仆寺旗| 尼玛县| 福建省| 财经| 合山市| 威宁| 调兵山市| 梁山县| 石门县| 盐源县| 资中县| 随州市| 衡阳市|