當(dāng)在Spring xml配置中定義Java bean或使用Spring注釋時,我們可以將Java bean標(biāo)記為 singleton 或原型。
如果一個Java bean被標(biāo)記為singleton,每次我們調(diào)用獲取bean,我們得到相同的實例。
如果一個Java bean被標(biāo)記為原型,每次我們將得到一個新的實例。
以下代碼定義了具有String類型屬性的Java bean。
package com.www.yjpub.cnmon;
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.yjpub.cnmon.MyService" />
</beans>
這里是運行上面定義的xml配置的代碼。
package com.www.yjpub.cnmon;
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è)置一個String值。
從輸出我們可以看到getMessage()調(diào)用都返回相同的消息,這意味著ApplicationContext使用 MyService 的一個副本。
如果bean是Spring IoC容器中的單例范圍,getBean()將總是返回相同的實例。
如果bean是Spring IoC容器中的單例范圍,getBean()將總是返回相同的實例。...
<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.yjpub.cnmon.MyService"
scope="prototype"/>
</beans>
再次運行

再次運行...
以下代碼顯示了如何使用注釋來標(biāo)記bean范圍。
package com.www.yjpub.cnmon;
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;
}
}
以下代碼顯示如何啟用自動組件掃描。
<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.yjpub.cnmon" />
</beans>
這里是新的App.java類來運行上面的配置。 customerService 更改為 myService 。Spring自動從類名創(chuàng)建一個bean名稱,小寫第一個信。
package com.www.yjpub.cnmon;
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());
}
}
再次運行

更多建議: