1. 程式人生 > >用MyEclipse開發Spring入門

用MyEclipse開發Spring入門

    開發環境:Myeclipse 4.1.1+Eclipse 3.1.2 +JDK 1.4.2+Tomcat 5.5

    編寫Spring開發指南中的第一個例子,這個用例子實現了大小寫的轉換!

1 新建一個專案

   File----->New ----->Project  在出現的對話方塊中選擇 MyEclipse 下的 Web Project,在Project Name 輸入mySpring1,其他的選項預設,再點Finish完成;

2 加入Spring 包

   在myspring1 專案上點右鍵,選MyEclipse ---> Add Spring Capabities,再點Finish完成;

3 加入Log4j 包

   選單--->Project -->Properties-----Java Build Path 點Libraries 選項卡,再選Add External Jars 加入log4j 的jar 包 (可從網上下)

http://archive.apache.org/dist/logging/log4j/1.2.8/

4 新建Action介面檔案

   右鍵點src ---->New ---->Interface (包名net.xiaxin.spring.qs)

   Action.java程式碼

package net.xiaxin.spring.qs;

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

}
5 建立Action介面的兩個實現UpperAction、LowerAction

LowerAction.java

package net.xiaxin.spring.qs;

public class LowerAction implements Action {
 
   private String message;
   public String getMessage() {
     return message;
    }
  
    public void setMessage(String string) {
      message = string;
    }
   
    public String execute(String str) {
     return (getMessage()+str).toLowerCase();
    }
  }

/////
UpperAction.java

package net.xiaxin.spring.qs;

public class UpperAction implements Action {
  
   private String message;
  
   public String getMessage() {
    return message;
   }
 
   public void setMessage(String string) {
     message = string;
   }
 
   public String execute(String str) {
    return (getMessage() + str).toUpperCase();
   }
 }

6  新建log4j.properties配置檔案,內容如下:

log4j.rootLogger=DEBUG, stdout
 
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%c{1} - %m%n

7  Spring配置檔案(bean.xml)

在myspring 上右擊 --->New--->File  檔名 bean.xml

bean.xml 內容如下

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
     <description>Spring Quick Start</description>
     <bean id="TheAction"
 class="net.xiaxin.spring.qs.UpperAction">
    <property name="message">
<value>HeLLo</value>
</property>
  </bean> 
 <bean id="action2" class="net.xiaxin.spring.qs.LowerAction">
<property name="message">
<value>HeLLo</value>
</property>
  </bean>
</beans>


8  建立測試檔案SimpleTest.java

package test;

import net.xiaxin.spring.qs.Action;

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("bean.xml");
    
     Action action = (Action) ctx.getBean("TheAction");
    
     System.out.println(action.execute("Rod Johnson"));
     action = (Action) ctx.getBean("action2");
     System.out.println(action.execute("jecKj"));
 
  }

}

9 把SimpleTeat.java 設定成主類就可以運行了,執行結果 (紅色部分是想要的結果)

FileSystemXmlApplicationContext - Publishing event in context [org.springframework.context.support.FileSystemXmlApplicationContext;hashCode=12694833]: org.springframework.context.event.ContextRefreshedEvent[source=org.springframework.context.support.FileSystemXmlApplicationContext: display name [org.springframework.context.support.FileSystemXmlApplicationContext;hashCode=12694833]; startup date [Wed May 31 13:30:25 CST 2006]; root of context hierarchy]
 DefaultListableBeanFactory - Returning cached instance of singleton bean 'TheAction'
 HELLOROD JOHNSON
DefaultListableBeanFactory - Returning cached instance of singleton bean 'action2'
 hellojeckj