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.blogjava.net/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"?>
<?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
 "http://www.springframework.org/dtd/spring-beans.dtd">
"http://www.springframework.org/dtd/spring-beans.dtd">
 <beans>
<beans>
 <bean id="helloWorldDAO1" class="HelloWorld1" />
    <bean id="helloWorldDAO1" class="HelloWorld1" />
 <bean id="helloWorldDAO2" class="HelloWorld2" />
    <bean id="helloWorldDAO2" class="HelloWorld2" />
 </beans>
</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 Sets,New,输入以下内容

 
之后Spring-Explorer出现以下内容

右键点击项目,选择Run as.. JUnit …
 

完成!
	posted on 2007-08-30 10:11 
张辰 阅读(905) 
评论(0)  编辑  收藏  所属分类: 
Dr. Oops