要学,但是左看右看看了些资料.一个直观带说明的例子或者最好.请不吝啬赐教.

解决方案 »

  1.   

    从hello world开始学吧:),从网上找的代码,自己配成功了。
    HelloWorldClient.java
    package com.openv.spring;import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;import org.springframework.beans.factory.BeanFactory;
    import org.springframework.beans.factory.xml.XmlBeanFactory;import org.springframework.core.io.ClassPathResource;
    import org.springframework.core.io.Resource;public class HelloWorldClient {
        protected static final Log log = LogFactory.getLog(HelloWorldClient.class);    public HelloWorldClient() {
            Resource resource = new ClassPathResource("appcontext.xml");
            BeanFactory factory = new XmlBeanFactory(resource);
            HelloWorld hw = (HelloWorld) factory.getBean("fileHelloWorld");
            log.info(hw.getContent());
        }    public static void main(String[] args) {
            new HelloWorldClient();
        }
    }HelloWorld.java
    package com.openv.spring;import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;public class HelloWorld {
        protected static final Log log = LogFactory.getLog(HelloWorld.class);    private HelloStr hStr;    public HelloWorld(HelloStr hStr) {
            this.hStr = hStr;
        }    public String getContent() {
            return hStr.getContent();
        }
    }HelloStr.java
    package com.openv.spring;public interface HelloStr {
        public String getContent();
    }FileHelloStr.java
    package com.openv.spring;import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;import java.util.Properties;public class FileHelloStr implements HelloStr {
        protected static final Log log = LogFactory.getLog(FileHelloStr.class);    private String propfilename;    public FileHelloStr(String propfilename) {
            this.propfilename = propfilename;
        }    public String getContent() {
            String helloworld = "";        try {
                Properties properties = new Properties();
                InputStream is = getClass().getClassLoader().getResourceAsStream(
                        propfilename);
                properties.load(is);
                is.close();
                helloworld = properties.getProperty("helloworld");
            } catch (FileNotFoundException ex) {
                log.error(ex);
            } catch (IOException ex) {
                log.error(ex);
            }        return helloworld;
        }
    }appcontext.xml(放在src根目录下)
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" 
       "http://www.springframework.org/dtd/spring-beans.dtd">
    <beans>     <bean name="fileHelloWorld"
              class="com.openv.spring.HelloWorld">
              <constructor-arg>
                   <ref bean="fileHello"/>
              </constructor-arg>    
         </bean>     <bean name="fileHello"
              class="com.openv.spring.FileHelloStr">
              <constructor-arg>
                   <value>helloworld.properties</value>
              </constructor-arg>  
         </bean>
         
    </beans>helloworld.properties(放在src根目录下)
    helloworld = "Hello World!!"build.xml(放在项目根目录下)
    <?xml version="1.0"?>
    <project name="example4" default="run" basedir=".">
    <path id="lib"> <fileset dir="./lib">
    <include name="spring.jar"/>
    <include name="commons-logging.jar"/>
    </fileset>
    </path> <target name="run" depends="compile" description="Run HelloWorldClient"> <java classname="com.openv.spring.HelloWorldClient" fork="yes">
    <classpath refid="lib"/>
    <classpath path="classes"/>
    </java> </target> <target name="compile"> <mkdir dir="classes"/> <javac destdir="classes" source="1.5" target="1.5"
    deprecation="false" optimize="false" failonerror="true">
    <src path="src"/>
    <classpath refid="lib"/>
    </javac>

    <copy todir="classes">
    <fileset dir="src">
    <include name="helloworld.properties"/>
    <include name="appcontext.xml"/>
    </fileset>
    </copy>
    </target>
    </project>
      

  2.   

    spring2.0 的包哪里下?需要哪些包?我正是想做个HelloWorld
      

  3.   


    这篇文章不错,讲的 Spring IOC 部分http://hi.baidu.com/iiitom/blog/item/86c09fa15804cc8b471064b2.html