There are several types of Bean's scope:
- Singleton in each spring IOC container, a bean defines only one instance of an object. The bean is initialized by default when the container starts, but we can specify the bean node's lazy-init= "true" to defer initializing the bean, so only the bean is initialized for the first time. Such as:
<bean id= ":" class= ":" lazy-init= "true"/>
If you want to apply lazy initialization to all beans, you can set default-lazy-init= "true" at the root node beans, as follows:
<beans default-lazy-init= "true" >
- prototype every time a bean gets a new object from the container
There are three other types of applications that are applied in Web project:
Request、
Session、
Global Session
Package Test.spring.jnit;import Org.junit.test;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;import Test.spring.service.impl.personservicebean;public class Beanscopetest {@Testpublic void Testscope () { ApplicationContext ApplicationContext = new Classpathxmlapplicationcontext ("Beans.xml");//------------------------ -------------------------Personservicebean personServiceBean1 = (Personservicebean) Applicationcontext.getbean (" Personservice "); Personservicebean personServiceBean2 = (Personservicebean) applicationcontext.getbean ("Personservice"); System.out.println (PersonServiceBean1 = = PersonServiceBean2);}}
This code returns true by the previous configuration, but if you change the scope of the Personservicebean
<bean id= "Personservice" class= "Test.spring.service.impl.PersonServiceBean" scope= "prototype" ></bean>
It will return false.
<bean id= "Personservice" class= "Test.spring.service.impl.PersonServiceBean" lazy-init= "false" init-method= "Init "Destroy-method=" ></bean> "destroy"
After the initialization of the bean is instantiated, the destruction releases the resource after the spring container is closed
Package Test.spring.service.impl;import Test.spring.service.personservice;public class Personservicebean implements Personservice {public void init () {System.out.println ("Initialize");} Public Personservicebean () {System.out.println ("instantiate Now");} @Overridepublic void Save () {System.out.println ("=============");} public void Destroy () {System.out.println ("Frees Resources");}}
Package Test.spring.jnit;import Org.junit.test;import Org.springframework.context.support.abstractapplicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;public class Beanscopetest {@Testpublic void Testscope () {//ApplicationContext ApplicationContext = new//classpathxmlapplicationcontext (//"beans.xml");// If scope= "Singleton", the Bean is now instantiated//System.out.println ("-------------------------------");/* * If scope= "prototype", Now instantiate the Bean. * If scope= "singleton", the Bean is instantiated at this time by setting lazy-init= "true" to delay the instantiation. *///Personservicebean personServiceBean1 = (Personservicebean)//applicationcontext//. Getbean ("Personservice");// Personservicebean personServiceBean2 = (Personservicebean)//applicationcontext//. Getbean ("Personservice");//// System.out.println (PersonServiceBean1 = = personServiceBean2);////return Trueabstractapplicationcontext Abstractapplicationcontext = new Classpathxmlapplicationcontext ("Beans.xml"); Abstractapplicationcontext.close ();}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. If you want to reprint, please specify the source: Http://blog.csdn.net/lindonglian
Spring (iv) configuration and life cycle of the JavaBean action range