在大型應用系統中,存在大量的工廠類。工廠類都是簡單的、僅提供靜態方法和變數的單一實例。他們將建立對象,並將這些對象綁定在一起,這樣就存在大量的重複代碼。
Spring最基本的一項功能就是:充當建立對象的工廠。其具體工作步驟如下:
1. 讀取並分析Spring設定檔(appcontext.xml放在classes目錄下)。
2.通過Java反射機制,建立並整合上述設定檔中定義的對象。
3.將建立的對象傳回給開發人員的應用代碼。因此,開發人員不用編寫工廠類。
註:Spring預設時僅建立單一實例的JavaBean,通過Spring設定檔中的bean元素的singleton屬性可以控制建立Java執行個體的方式。
appcontext.xml設定檔執行個體如下:
[c-sharp] view plain copy <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean name="fileHelloWorld" class="testspring.sample.HelloWorld"> <constructor-arg> <ref bean="fileHello"/> </constructor-arg> </bean> <bean name="fileHello" class="testspring.sample.HelloWorldStr"> <constructor-arg> <value>helloworld.properties</value> </constructor-arg> </bean> </beans>
上述代碼中,聲明了一個名為fileHelloWorld的HelloWorld執行個體,該執行個體的構造方法需要傳入一個HelloWorldStr類的執行個體作為參數。constructor-arg元素宣告構造方法需要用到的參數,通過ref元素可以引用到Spring設定檔中的其他已定義的JavaBean。value元素直接擷取參數。比如上例中,HelloWorldStr類接收“helloworld.properties”作為參數。
下面例子展示了一個簡單的Spring程式。例子的功能是讀取一個設定檔,並將檔案裡的內容輸出到控制台。
[java] view plain copy package testspring.sample; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class HelloWorldStr { private String filename; public HelloWorldStr(String filename) { this.filename = filename; } public String getContent() { String helloWorld = ""; try { Properties properties = new Properties(); InputStream in = this.getClass().getClassLoader().getResourceAsStream(filename); properties.load(in); in.close(); helloWorld = properties.getProperty("helloworld"); } catch (IOException e) { e.printStackTrace(); } return helloWorld; } }
HelloWorld.java:
[java] view plain copy package testspring.sample; public class HelloWorld { HelloWorldStr str; public HelloWorld(HelloWorldStr str){ this.str = str; } public String getContent(){ return str.getContent(); } }
HelloWorldClient.java:
[java] view plain copy package testspring.sample; import java.io.IOException; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; public class HelloWorldClient { /** *<p>Description:<p> * @param args * @author mayk 2009-4-28 * @throws IOException */ public static void main(String[] args) throws IOException { // 1. 讀取並分析Spring設定檔('appcontext.xml')。 Resource resource = new ClassPathResource("appcontext.xml"); // 2. 通過Java反射機制,建立並整合上述設定檔中定義的對象。 BeanFactory factory = new XmlBeanFactory(resource); // 3. 將建立的對象傳回給開發人員的應用代碼。因此,開發人員不用編寫工廠類。 HelloWorld helloworld = (HelloWorld)factory.getBean("fileHelloWorld"); System.out.println(helloworld.getContent()); // } }
appcontext.xml檔案位於src目錄下。
[c-sharp] view plain copy <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean name="fileHelloWorld" class="testspring.sample.HelloWorld"> <constructor-arg> <ref bean="fileHello"/> </constructor-arg> </bean> <bean name="fileHello" class="testspring.sample.HelloWorldStr"> <constructor-arg> <value>helloworld.properties</value> </constructor-arg> </bean> </beans>
helloworld.properties檔案內容為:
helloworld=Hi,Spring