當(dāng)在Spring xml配置中定義Java bean或使用Spring注釋時(shí),我們可以將Java bean標(biāo)記為 singleton
或原型
。
如果一個(gè)Java bean被標(biāo)記為singleton,每次我們調(diào)用獲取bean,我們得到相同的實(shí)例。
如果一個(gè)Java bean被標(biāo)記為原型,每次我們將得到一個(gè)新的實(shí)例。
以下代碼定義了具有String類型屬性的Java bean。
package com.www.o2fo.common; public class MyService { String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
如果在bean配置文件中未指定bean作用域,則默認(rèn)為singleton。
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="customerService" class="com.www.o2fo.common.MyService" /> </beans>
這里是運(yùn)行上面定義的xml配置的代碼。
package com.www.o2fo.common; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main( String[] args ) { ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"SpringBeans.xml"}); MyService customerA = (MyService)context.getBean("customerService"); customerA.setMessage("Message by customerA"); System.out.println("Message : " + customerA.getMessage()); //retrieve it again MyService custB = (MyService)context.getBean("customerService"); System.out.println("Message : " + custB.getMessage()); } }
輸出
Message : Message by customerA Message : Message by customerA
上面的代碼從 ApplicationContext
中獲取兩次 MyService
。并且它在第一次調(diào)用后為消息設(shè)置一個(gè)String值。
從輸出我們可以看到getMessage()調(diào)用都返回相同的消息,這意味著ApplicationContext使用 MyService
的一個(gè)副本。
如果bean是Spring IoC容器中的單例范圍,getBean()將總是返回相同的實(shí)例。
如果bean是Spring IoC容器中的單例范圍,getBean()將總是返回相同的實(shí)例。...
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="customerService" class="com.www.o2fo.common.MyService" scope="prototype"/> </beans>
再次運(yùn)行
再次運(yùn)行...
以下代碼顯示了如何使用注釋來標(biāo)記bean范圍。
package com.www.o2fo.common; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Service; @Service @Scope("prototype") public class MyService { String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
以下代碼顯示如何啟用自動(dòng)組件掃描。
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:component-scan base-package="com.www.o2fo.common" /> </beans>
這里是新的App.java類來運(yùn)行上面的配置。 customerService
更改為 myService
。Spring自動(dòng)從類名創(chuàng)建一個(gè)bean名稱,小寫第一個(gè)信。
package com.www.o2fo.common; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main( String[] args ) { ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"SpringBeans.xml"}); MyService customerA = (MyService)context.getBean("myService"); customerA.setMessage("Message by customerA"); System.out.println("Message : " + customerA.getMessage()); //retrieve it again MyService custB = (MyService)context.getBean("myService"); System.out.println("Message : " + custB.getMessage()); } }
再次運(yùn)行
更多建議: