提供了三種方式實例化Bean. * 構造方法實例化:(默認無參數) * 靜態工廠實例化: * 實例工廠實例化:
<!-- 默認狀況下使用的就是無參數的構造方法. --> <bean id="bean1" class="cn.spring.demo2.Bean1"></bean>
package cn.spring.demo2; /* * 使用無參數的構造方法實例化 */ public class Bean1 { public Bean1(){}; }
<!-- 靜態工程實例化 --> <bean id="bean2" class="cn.spring.demo2.Bean2Factory" factory-method="getBean2"></bean>
package cn.spring.demo2; /* * 使用靜態工廠實例化 */ public class Bean2 { }
package cn.spring.demo2; /* * Bean2的靜態工程 */ public class Bean2Factory { public static Bean2 getBean2() { System.out.println("靜態工程或得bean2的方法……"); return new Bean2(); } }
<!-- 使用實例工程實例化 --> <bean id="bean3" factory-bean="bean3Factroy" factory-method="getBean3"></bean> <bean id="bean3Factroy" class="cn.spring.demo2.Bean3Factory"></bean>
package cn.spring.demo2; /* * 使用實例工廠實例化 */ public class Bean3 { }
package cn.spring.demo2; /** * 使用實例方法 * * @author NOP * */ public class Bean3Factory { public Bean3 getBean3() { System.out.println("實例工廠得bean3的方法……"); return new Bean3(); } }
package cn.spring.demo2; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SprintTest2 { @Test // 無參數的構造方法實例化 public void demo1() { ApplicationContext applicaionContext = new ClassPathXmlApplicationContext( "applicationContext.xml"); Bean1 bean1 = (Bean1) applicaionContext.getBean("bean1"); System.out.println(bean1); } @Test // 靜態工廠實例化 public void demo2() { ApplicationContext applicaionContext = new ClassPathXmlApplicationContext( "applicationContext.xml"); Bean2 bean2 = (Bean2) applicaionContext.getBean("bean2"); System.out.println(bean2); } @Test // 實例工程實例化 public void demo3() { ApplicationContext applicaionContext = new ClassPathXmlApplicationContext( "applicationContext.xml"); Bean3 bean3 = (Bean3) applicaionContext.getBean("bean3"); System.out.println(bean3); } }
如今的開發中都使用id屬性便可.web
實際開發中主要使用singleton,prototypespring
package cn.spring.demo3; public class Customer { public Customer(){ super(); System.out.println("Customer類實例化……"); } }
package cn.spring.demo3; public class Product { private String name ; public void setName(String name) { this.name = name; } public void setup(){ System.out.println("初始化……"); } public void teardown(){ System.out.println("銷燬……"); } @Override public String toString() { return "Product [name=" + name + "]"; } }
<!-- cn.spring.demo3 bean做用範圍--> <!--默認是單實例--> <bean id="customer" class="cn.spring.demo3.Customer" scope="singleton"></bean> <!-- 測試類結果:單實例對象同樣 Customer類實例化…… cn.spring.demo3.Customer@19e215b cn.spring.demo3.Customer@19e215b --> <!-- 多實例 --> <bean id="customer" class="cn.spring.demo3.Customer" scope="prototype"></bean> <!-- 測試類結果:多實例對象不同 Customer類實例化…… cn.spring.demo3.Customer@19762f Customer類實例化…… cn.spring.demo3.Customer@19e215b --> <!-- bean的生命週期--> <bean id="Product" class="cn.spring.demo3.Product" init-method="setup" destroy-method="teardown"> <property name="name" value="空調"/> </bean> <!-- cn.spring.demo3 bean做用範圍-->
package cn.spring.demo3; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SprintTest3 { @Test // 測試單實例或多實例 public void demo1() { ApplicationContext applicationContext = new ClassPathXmlApplicationContext( "applicationContext.xml"); Customer c1 = (Customer) applicationContext.getBean("customer"); System.out.println(c1); Customer c2 = (Customer) applicationContext.getBean("customer"); System.out.println(c2); } @Test public void demo2() { //ApplicationContext只會執行 init-method="setup" 方法 ApplicationContext applicationContext = new ClassPathXmlApplicationContext( "applicationContext.xml"); Product p1 = (Product) applicationContext.getBean("Product"); /* *結果: *初始化…… */ // 執行銷燬的方法用ClassPathXmlApplicationContext有close的方法 ClassPathXmlApplicationContext applicationContext1 = new ClassPathXmlApplicationContext( "applicationContext.xml"); Product p2 = (Product) applicationContext.getBean("Product"); System.out.println(p2); // 會執行destroy-method="teardown" applicationContext1.close(); /* *結果: *初始化…… *銷燬...... */ } }