初学一种新的技术,都是从简单的程序入手为佳.
怎么来写一个Spring的简单程序呢?
 
0.先将spring.jar放到构建路径中,如果是web项目的copy到lib目录下.
 
1.写一个java的bean,要求有一个属性对应有get,set方法的;又或者是在该bean的构造函数中设置该属性.
   前者的依赖注入方式是,set注入,后者为构造注入.
 
view plaincopy to clipboardprint?
package com.gc.action;   
  
public class HelloWorld {   
  
    public String msg = null;   
  
    public String getMsg() {   
        return msg;   
    }   
  
    public void setMsg(String msg) {   
        this.msg = msg;   
    }   
       
}  
package com.gc.action;
public class HelloWorld {
 public String msg = null;
 public String getMsg() {
  return msg;
 }
 public void setMsg(String msg) {
  this.msg = msg;
 }
 
} 
或
view plaincopy to clipboardprint?
package com.gc.action;   
  
public class HelloWorld {   
  
    public String msg = null;   
  
    public String getMsg() {   
        return msg;   
    }   
  
    public void setMsg(String msg) {   
        this.msg = msg;   
    }   
  
    public HelloWorld(String msg) {   
           
        this.msg = msg;   
    }   
  
}  
package com.gc.action;
public class HelloWorld {
 public String msg = null;
 public String getMsg() {
  return msg;
 }
 public void setMsg(String msg) {
  this.msg = msg;
 }
 public HelloWorld(String msg) {
  
  this.msg = msg;
 }
} 
 
2.写一配置文件config.xml
view plaincopy to clipboardprint?
<?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="hello" class="com.gc.action.HelloWorld">  
                    <property name="msg">  
                            <value>HelloWold</value>  
                    </property>  
            </bean>  
 </beans>  
<?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="hello" class="com.gc.action.HelloWorld">
      <property name="msg">
        <value>HelloWold</value>
      </property>
    </bean>
 </beans> 
或
view plaincopy to clipboardprint?
<?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="hello" class="com.gc.action.HelloWorld">  
                    <constructor-arg index="0">  
                    <value>hello china </value>  
                    </constructor-arg>  
            </bean>  
 </beans>  
<?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="hello" class="com.gc.action.HelloWorld">
      <constructor-arg index="0">
      <value>hello china </value>
      </constructor-arg>
    </bean>
 </beans> 
 
3.在需要的类中用配置好的bean
view plaincopy to clipboardprint?
package com.gc.test;   
import com.gc.action.HelloWorld;   
import org.springframework.context.ApplicationContext;   
import org.springframework.context.support.FileSystemXmlApplicationContext;   
  
  
public class TestHolloWorld {   
  
    /**  
     * @param args  
     */  
    public static void main(String[] args) {   
        // TODO Auto-generated method stub   
  
        //HelloWorld hello = new HelloWorld();   
        //hello.setMsg("hello world");   
        //System.out.print(hello.getMsg());   
           
        ApplicationContext actx = new FileSystemXmlApplicationContext("WebRoot//config.xml");   
        HelloWorld hello = (HelloWorld) actx.getBean("hello");   
        System.out.print(hello.getMsg());   
           
    }   
  
}  
package com.gc.test;
import com.gc.action.HelloWorld;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class TestHolloWorld {
 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  //HelloWorld hello = new HelloWorld();
  //hello.setMsg("hello world");
  //System.out.print(hello.getMsg());
  
  ApplicationContext actx = new FileSystemXmlApplicationContext("WebRoot//config.xml");
  HelloWorld hello = (HelloWorld) actx.getBean("hello");
  System.out.print(hello.getMsg());
  
 }
} 
 
 
发表于 @ 2009年03月18日 23:06:00