詳細(xì)的 spring 入門實(shí)例講解運(yùn)用
2024-07-21 02:15:01
供稿:網(wǎng)友
中國最大的web開發(fā)資源網(wǎng)站及技術(shù)社區(qū),
首先我們要先取得spring的相關(guān)檔案,spring的檔案放在sourceforge上,網(wǎng)址是:
http://sourceforge.net/project/showfiles.php?group_id=73357
撰寫此文時,spring最新的版本是1.1.1,有兩個下載版本,一個是spring-framework-1.1.1-with- dependencies.zip,一個是spring-framework-1.1.1.zip,with-dependencies的包括一些 ant、jakarta-commons、struts、velocity等等其它開源java項(xiàng)目的相依檔案,如果您也需要這些相關(guān)檔案,可以下載這個版本,如果您已經(jīng)有這些相關(guān)檔案,則只需要下載spring-framework-1.1.1.zip這個檔案。
下載zip檔案并解壓縮之后,在dist目錄下就是使用spring所需要的相關(guān)檔案,如果下載的是with-dependencies版本,則在 lib目錄中的是您可能會用到的相依檔案。在dist目錄下,spring-core.jar是spring的核心,對于撰寫簡單的單機(jī)程序來說,使用這個核心即可,如果日后需要使用到spring其它的子框架支持,再將其它的jar檔案加入即可,例如spring-aop.jar、spring- webmvc.jar等等。您也可以直接使用spring.jar這個檔案,它包括了所有spring支持的功能所需要的所有類別,而不再需要加入個別的 jar檔案。
就我們的第一個spring程序,只要spring-core.jar這個檔案即可,它唯一相依的其它項(xiàng)目檔案,是commons- logging.jar,您可以在lib目錄的jakarta-commons目錄中找到,將這兩個檔案的位置加入至classpath中,我們就可以開始撰寫第一個spring程序。
來撰寫我們的第一個組件(component),它只是一個簡單的javabean,用來向新的使用者打招呼:
hellobean.java
package onlyfun.caterpillar;
public class hellobean {
private string helloword = "hello!world!";
public void sethelloword(string helloword) {
this.helloword = helloword;
}
public string gethelloword() {
return helloword;
}
}
hellobean有預(yù)設(shè)的"hello!world!"字符串,我們也可以透過setter來設(shè)定新的招呼語,不過我們不親自撰寫程序來作這些事,而是在組態(tài)檔案定義,由spring來為我們作設(shè)定的動作,我們撰寫bean.xml:
bean.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="onlyfun.caterpillar.hellobean">
<property name="helloword"><value>hello!justin!</value></property>
</bean>
</beans>
bean.xml中定義了javabean的別名與來源類別,<property>標(biāo)簽中設(shè)定了我們希望注入至javabean的字符串值,bean.xml必須在您的classpath可以存取到的目錄中,也許是現(xiàn)行的工作目錄,在web程序中可以是在classes目錄下,我們這邊使用的是單機(jī)程序的方式,將使用fileinputstream讀取bean.xml,所以將之置于現(xiàn)行的工作目錄中,接著我們撰寫一個簡單的測試程序:
springtest.java
package onlyfun.caterpillar;
import java.io.*;
import org.springframework.beans.factory.beanfactory;
import org.springframework.beans.factory.xml.xmlbeanfactory;
public class springtest {
public static void main(string[] args) throws ioexception {
inputstream is = new fileinputstream("bean.xml");
beanfactory factory = new xmlbeanfactory(is);
hellobean hello = (hellobean) factory.getbean("hellobean");
system.out.println(hello.gethelloword());
}
}
這是從比較低層次的角度來使用spring的ioc容器功能,藉由beanfactory來讀取組態(tài)檔案并完成依賴的關(guān)聯(lián)注入,這邊的依賴是什么?指的是 hellobean相依于string對象,透過setter所保留的接口,我們使用setter injection來完成這個依賴注入,而不是將招呼語寫死在hellobean,beanfactory是整個spring的重點(diǎn)所在,整個 spring的核心都圍繞著它,在這邊使用的是xmlbeanfactory,負(fù)責(zé)讀取xml組態(tài)檔案,當(dāng)然我們也可以使用properties檔案,這之后會再介紹。
beanfactory讀取bean的組態(tài)設(shè)定并完成關(guān)系維護(hù)之后,我們可以藉由getbean()方法并指定bean的別名來取得實(shí)例,來看看實(shí)際運(yùn)行之后的效果:
2004/10/21 上午 10:28:00 org.springframework.beans.factory.xml.xmlbeandefinitionreader loadbeandefinitions
信息: loading xml bean definitions from resource for inputstream
2004/10/21 上午 10:28:00 org.springframework.beans.factory.support.abstractbeanfactory getbean
信息: creating shared instance of singleton bean 'hellobean'
hello!justin!