這幾天都在學習怎麼使用SSH,首先是從Spring開始的,一些程式碼都是參考《Java Web開發技術大全--JSP+Servlet+Struts+Hibernate+Spring+AJAX》這本書寫的。
一.Spring的核心技術
1.反向控制(Inversion of Control,Ioc)和依賴注入
任何有應用價值的系統都至少有兩個類來互相配合工作,通過由一個主要的入口類來啟動程式,然後在這個類中建立另一個類的對象執行個體,並進行相應的操作。這種工作方式是由調用者主動建立的對象執行個體,是主動的工作方式。
而如果使用Ioc,建立對象的任務並不是由調用者來完成的,而是通過外部的協調者(在Spring中是Spring Ioc容器)來完成的。因此也可以認為調用者要依賴Spring Ioc容器來獲得(或者稱為注入)對象執行個體,所以也可以將Ioc稱為依賴注入。
2.面向方面編程(AOP)
二.執行個體
手動建立的Java Project是不帶Spring功能的,需要我們通過手動設定來完成。具體操作是:myecplise->project capabilities->add spring capabilities。這樣project下就有了一個applicationContext.xml檔案。Spring就是通過配置該xml來實現其功能的。
Spring模式從簡單來講就是一個介面一個實作類別,然後還有一個測試類別。
我們這裡建立一個介面HelloService,實作類別HelloServiceImpl,測試類別FirstSpring。
HelloService.java
package chapter22;//建立一個HelloService介面,Spring架構中的介面類。public interface HelloService {public String getGreeting();}
HelloServiceImpl.java
package chapter22;//建立HelloService介面的實作類別HelloServiceImpl,這個類中的屬性將成為Bean被裝配。public class HelloServiceImpl implements HelloService {private String greeting;// JavaBean,HelloServiceImpl類的對象執行個體會通過// Spring的xml設定檔來建立public String getGreeting()// get方法{System.out.println("getGreeting()方法被調用");return greeting;}public void setGreeting(String greeting)// set方法{this.greeting = greeting;System.out.println("setGreeting()方法被調用");}}
FirstSpring.java
package chapter22;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.context.support.FileSystemXmlApplicationContext;//使用ApplicationContext配置JavaBeanpublic class FirstSpring {public static void main(String args[]) {// 方案一:使用Spring。ApplicationContext ctx = new FileSystemXmlApplicationContext("src//applicationContext.xml");HelloService hello = (HelloService) ctx.getBean("helloservice");System.out.println(hello.getGreeting());// 方案二:不適用Spring。HelloServiceImpl hello2 = new HelloServiceImpl();hello2.setGreeting("Xu Wei");System.out.println(hello2.getGreeting());// 方案三:使用Spring,ClassPathXmlApplicationContextApplicationContext ctx2 = new ClassPathXmlApplicationContext("applicationContext.xml");HelloService hello3 = (HelloService) ctx2.getBean("helloservice");System.out.println(hello3.getGreeting());// 結論:// 方案一和方案二輸出的結果是一樣的,其實實現的本質也是一樣的,// 唯一的區別就是建立HelloServiceImpl類執行個體對象的方法不一樣。// 方案一時通過Spring的xml檔案配置來得到,而方案二則是直接通過代碼來建立。// FileSystemXmlApplicationContext:通過絕對或相對路徑制定XML設定檔,並裝載XML檔案的配置資訊。// ClassPathXmlApplicationContext:從類路徑中搜尋XML設定檔。可以使用FileSystemXmlApplicationContext類// 替換ClassPathXmlApplicationContext,但要將構造方法的參數值改為"applicationContext.xml"}}
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><bean id="helloservice" class="chapter22.HelloServiceImpl"><property name="greeting" value="Xu Wei"></property></bean></beans>
最後右鍵FirstSpring->run as->java application。第一個spring程式就完成了。
PS:手動設定JavaBean有兩種方式,上面提到的都是應用上下文(ApplicationContext)來配置的,下面介紹使用BeanFactory來進行配置的方法。
HelloService.java和HelloServiceImpy.java都不需要改變,只需要改變測試類別。
TeatBeanFactory.java
package chapter22;import org.springframework.beans.factory.BeanFactory;import org.springframework.beans.factory.xml.*;import org.springframework.core.io.FileSystemResource;//Bean工廠(BeanFactory)手動設定JavaBean。public class TestBeanFactory {public static void main(String args[]) {BeanFactory factory = new XmlBeanFactory(new FileSystemResource("src//applicationContext.xml"));HelloService hello = (HelloService) factory.getBean("helloservice");System.out.println(hello.getGreeting());}/* * 單從裝配Bean上看,ApplicationContext和BeanFactory類似,單ApplicationContext比BeanFactory提供了更多的功能 * ,如國際化,裝載檔案資源、向監聽器Bean發送事件等。如此,如果要使用更多的功能,最好使用ApplicationContext來裝配Bean * * 裝配Bean實際上就是在XML中設定檔JavaBean的相關資訊,然後由Spring架構讀取該設定檔,並建立相應的JavaBean對象執行個體 * 的過程。通常來講,需要用手工的方式制定Spring裝配JavaBean時的動作,如調用哪一個構造方法,初始化指定的JavaBean屬性等。 */}