Monday, August 29, 2011

Simple Spring

  1. Download spring framework from (http://www.springsource.org/download) and commons-logging-1.1.1 from (http://commons.apache.org/logging/download_logging.cgi)
  2. Unzip the spring-framework-3.1.0.M2-with-docs.zip and commons-logging-1.1.1-src.zip
  3. Import all jar files contained in dist folder and commons-logging-1.1.1.jar into your project.
  4. create Hellobean.java
  • public class HelloBean {
        private String helloWord;
        public void setHelloWord(String helloWord) {
            this.helloWord = helloWord;
        }
        public String getHelloWord() {
            return helloWord;
        }
    }
   5.  create applicationContext.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="helloBean" class="com.cht.paas.springtest.HelloBean">
            <property name="helloWord">
                <value>Hello Hello Testing</value>
            </property>
        </bean>
    </beans>
   6.  create demo java:
  • import org.springframework.core.io.FileSystemResource;
    import org.springframework.core.io.Resource;
    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.beans.factory.xml.XmlBeanFactory;
    public class SpringDemo {
        public static void main(String[] args) {
            Resource rs = new FileSystemResource("applicationConfig.xml");
            BeanFactory factory = new XmlBeanFactory(rs);
            HelloBean hello = (HelloBean) factory.getBean("helloBean");
            hello.setHelloWord("This is setting hello world");
            System.out.println(hello.getHelloWord());  
        }
    }
References:
  1. http://caterpillar.onlyfun.net/Gossip/SpringGossip/SpringGossip.html
  2. http://www.springsource.org/download
  3. http://www.ibm.com/developerworks/cn/java/wa-spring1/

No comments:

Post a Comment