posts - 32,comments - 8,trackbacks - 0

          Oops! Spring Framework Quick Start!


          eclipse europa + tomcat 5.5+spring 2.06+lomboz S3.3RC1


          Purpose:

          完成這個項目,能夠對spring框架有個整體認識,包括IoC之類的。

          Prerequisite:

          eclipse-java-europa-win32.zip

          apache-tomcat-5.5.23.exe

          tomcatPluginV31.zip

          spring-framework-2.0.6-with-dependencies.zip

          org.objectweb.lomboz-and-prereqs-S-3.3RC1-200708181505.zip


          Reference:

          http://www.aygfsteel.com/pixysoft/archive/2007/08/29/141048.html 



          Chapter 01
           

          新建一個Java Project,項目名為OopsSpringFramework

           

           

           

          選擇project – properties – Libraries添加以下類庫。所有類庫可以在spring-framework-2.0.6.zip里面找到,包括dist目錄和lib目錄里面。

           

           

           

          src目錄下面添加以下文件:



          beanRefDataAccess.xml

          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
          "http://www.springframework.org/dtd/spring-beans.dtd"
          >
          <beans>
              
          <bean id="helloWorldDAO1" class="HelloWorld1" />
              
          <bean id="helloWorldDAO2" class="HelloWorld2" />
          </beans>


          beanRefFactory.xml

          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
          "http://www.springframework.org/dtd/spring-beans.dtd"
          >
          <beans>
              
          <bean id="beanFactory"
                  class
          ="org.springframework.context.support.ClassPathXmlApplicationContext">
                  
          <constructor-arg>
                      
          <list>
                          
          <value>beanRefDataAccess.xml</value>
                          
          <value>beanRefService.xml</value>
                          
          <value>beanRefMVC.xml</value>
                      
          </list>
                  
          </constructor-arg>
              
          </bean>
          </beans>


          beanRefMVC.xml

          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE beans PUBLIC
          "-//SPRING//DTD BEAN//EN"
          "http://www.springframework.org/dtd/spring-beans.dtd"
          >
          <beans>
              
          <bean id="helloWorldMVC1" class="HelloWorld1" />
              
          <bean id="helloWorldMVC2" class="HelloWorld2" />
          </beans>

          beanRefService.xml

          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
          "http://www.springframework.org/dtd/spring-beans.dtd"
          >
          <beans>
              
          <bean id="helloWorld1" class="HelloWorld1" />
              
          <bean id="helloWorld2" class="HelloWorld2" />
              
          <bean id="springDemoConstructor" class="SpringDemoConstructor">
                  
          <constructor-arg>
                      
          <value>Spring IDE Constructor</value>
                  
          </constructor-arg>
                  
          <property name="helloWorld">
                      
          <ref bean="helloWorld1"></ref>
                  
          </property>
              
          </bean>
              
          <bean id="springDemoSetter" class="SpringDemoSetter">
                  
          <property name="hello" value="Spring IDE Setter" />
                  
          <property name="helloWorld">
                      
          <ref bean="helloWorld2"></ref>
                  
          </property>
              
          </bean>
          </beans>


          HelloWorld1.java

          public class HelloWorld1 implements IHelloWorld
          {
              
          public HelloWorld1()
              {
                  
          super();
              }

              
          public String sayHelloWorld()
              {
                  
          return "Hello World HelloWorld1";
              }
          }

          HelloWorld2.java

          public class HelloWorld2 implements IHelloWorld
          {
              
          public HelloWorld2()
              {
                  
          super();
              }

              
          public String sayHelloWorld()
              {
                  
          return "Hello World HelloWorld2";
              }
          }

          IHelloWorld.java

           

          public interface IHelloWorld
          {
              String sayHelloWorld();
          }


          ISpringDemo.java

          public interface ISpringDemo
          {
              IHelloWorld getHelloWorld();

              String getHello();
          }


          ServiceFactory.java

          import org.springframework.beans.factory.BeanFactory;
          import org.springframework.beans.factory.access.BeanFactoryLocator;
          import org.springframework.beans.factory.access.BeanFactoryReference;
          import org.springframework.beans.factory.access.SingletonBeanFactoryLocator;

          public final class ServiceFactory
          {
              
          private static BeanFactoryLocator bfLocator = null;
              
          private static BeanFactoryReference bfReference = null;
              
          private static BeanFactory factory = null;
              
          static
              {
                  bfLocator 
          = SingletonBeanFactoryLocator.getInstance();
                  bfReference 
          = bfLocator.useBeanFactory("beanFactory");
                  factory 
          = bfReference.getFactory();
              }

              
          private ServiceFactory()
              {
                  
          super();
              }

              
          public static Object getBeanByName(final String beanName)
              {
                  
          return factory.getBean(beanName);
              }
          }

          SpringDemoConstructor.java

          public class SpringDemoConstructor implements ISpringDemo
          {
              
          private String hello;
              
          private IHelloWorld helloWorld;

              
          public SpringDemoConstructor(String hello)
              {
                  
          this.hello = hello;
              }

              
          public String getHello()
              {
                  
          return hello;
              }

              
          public IHelloWorld getHelloWorld()
              {
                  
          return helloWorld;
              }

              
          public void setHelloWorld(IHelloWorld helloWorld)
              {
                  
          this.helloWorld = helloWorld;
              }
          }

          SpringDemoSetter.java

          public class SpringDemoSetter implements ISpringDemo
          {
              
          private String hello;
              
          private IHelloWorld helloWorld;

              
          public String getHello()
              {
                  
          return hello;
              }

              
          public void setHello(String hello)
              {
                  
          this.hello = hello;
              }

              
          public IHelloWorld getHelloWorld()
              {
                  
          return helloWorld;
              }

              
          public void setHelloWorld(IHelloWorld helloWorld)
              {
                  
          this.helloWorld = helloWorld;
              }
          }


          SpringIDETest.java

          import junit.framework.TestCase;

          public class SpringIDETest extends TestCase
          {
              
          private IHelloWorld helloWorld = null;
              
          private ISpringDemo springDemo = null;
              
          private final static String hello1 = "Hello World HelloWorld1";
              
          private final static String hello2 = "Hello World HelloWorld2";
              
          private final static String helloset = "Spring IDE Setter";
              
          private final static String hellocon = "Spring IDE Constructor";

              
          public void testSpringBeans()
              {
                  helloWorld 
          = (IHelloWorld) ServiceFactory.getBeanByName("helloWorld1");
                  assertEquals(hello1, helloWorld.sayHelloWorld());
                  helloWorld 
          = (IHelloWorld) ServiceFactory.getBeanByName("helloWorld2");
                  assertEquals(hello2, helloWorld.sayHelloWorld());
              }

              
          public void testIoCConstructor()
              {
                  
          // Constructor
                  springDemo = (ISpringDemo) ServiceFactory
                          .getBeanByName(
          "springDemoConstructor");
                  assertEquals(hellocon, springDemo.getHello());
                  assertEquals(hello1, springDemo.getHelloWorld().sayHelloWorld());
              }

              
          public void testIoCSetter()
              {
                  
          // Setter
                  springDemo = (ISpringDemo) ServiceFactory
                          .getBeanByName(
          "springDemoSetter");
                  assertEquals(helloset, springDemo.getHello());
                  assertEquals(hello2, springDemo.getHelloWorld().sayHelloWorld());
              }
          }




          鼠標右點擊OopsSpringFramework,選擇 Add Spring Project Nature


           

          打開Spring Explorer窗口

           

           

           



          SpringExplorer里面右選擇項目,properties.



          選擇
          Beans Support,Add xml



           

          之后得到以下內容


           

          選擇Config SetsNew,輸入以下內容

           

          之后Spring-Explorer出現以下內容



          右鍵點擊項目,選擇Run as.. JUnit …

           


          完成!


          posted on 2007-08-30 10:11 張辰 閱讀(892) 評論(0)  編輯  收藏 所屬分類: Dr. Oops
          主站蜘蛛池模板: 仁寿县| 怀远县| 麻阳| 双牌县| 榕江县| 武夷山市| 玉树县| 金坛市| 和龙市| 筠连县| 英山县| 贵州省| 林西县| 普格县| 璧山县| 大竹县| 宁海县| 柳林县| 伊吾县| 铜梁县| 杭锦旗| 眉山市| 饶阳县| 五指山市| 邹城市| 芜湖县| 南木林县| 杭州市| 宜章县| 得荣县| 甘孜| 旅游| 洪泽县| 崇明县| 安丘市| 双桥区| 北海市| 新安县| 锡林浩特市| 日照市| 怀安县|