初學一種新的技術,都是從簡單的程序入手為佳.
怎么來寫一個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