初學(xué)一種新的技術(shù),都是從簡(jiǎn)單的程序入手為佳.
怎么來(lái)寫(xiě)一個(gè)Spring的簡(jiǎn)單程序呢?
0.先將spring.jar放到構(gòu)建路徑中,如果是web項(xiàng)目的copy到lib目錄下.
1.寫(xiě)一個(gè)java的bean,要求有一個(gè)屬性對(duì)應(yīng)有g(shù)et,set方法的;又或者是在該bean的構(gòu)造函數(shù)中設(shè)置該屬性.
前者的依賴注入方式是,set注入,后者為構(gòu)造注入.
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.寫(xiě)一配置文件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());
}
}
發(fā)表于 @ 2009年03月18日 23:06:00