the timing of the bean's initialization
The scope of the spring container-managed bean is explained earlier. Then we have to think about a question: When did the bean actually instantiate it? We use this issue as a primer to start the description of this article.
The Bean object is instantiated at the following two moments: when the Getbean () method is called. When the spring container starts.
So, at what point the Bean object is instantiated, there is a connection to the scope of the bean . We delve into the case of the scope of the bean that is configured for spring management . To be able to clearly see the instantiation of the Bean object, we need to modify the code of the Personservicebean class to:
public class Personservicebean implements Personservice {public
Personservicebean () {
System.out.println (" I was instantiated ");
}
@Override public
Void Save () {
System.out.println ("I Am the Save () method");}
}
When spring's configuration file--beans.xml the contents of:
<?xml version= "1.0" encoding= "UTF-8"?> <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.xsd ">
<bean id= "Personservice" class= "Cn.itcast.service.impl.PersonServiceBean" ></bean>
</ Beans>
That is, when the scope of the bean is singleton, we modify the code of the Springtest class to:
public class Springtest {
@Test public
void Test () {
ApplicationContext ctx = new Classpathxmlapplicationcontext ("Beans.xml"); Instantiate spring Container
}
}
At this point, the test () method, Eclipse console output:
I was instantiated.
This means that when the bean's scope is singleton, the Bean object is created when the spring container is started. That is, by default, the bean is initialized when the container starts, but we can also specify the bean node's lazy-init= "true" to defer the initialization of the bean, when the bean is initialized only the first time it is fetched. Such as:
We will change the contents of the spring configuration file--beans.xml to:
<?xml version= "1.0" encoding= "UTF-8"?> <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.xsd ">
<bean id= "Personservice" class= "Cn.itcast.service.impl.PersonServiceBean" lazy-init= "true" ></bean >
</beans>
At this point, the test () method is tested, and the Eclipse console does not output this sentence at all:
I was instantiated.
Lazy-init= "True" specifies that the bean should not be instantiated when the spring container is started.
At this point, only the code for the Springtest class is modified to:
public class Springtest {
@Test public
void Test () {
ApplicationContext ctx = new Classpathxmlapplicationcontext ("Beans.xml"); Instantiate the Spring container
personservice personservice = (personservice) ctx.getbean ("Personservice");//Get beans from the spring container
}
}
Testing the test () method again, the Eclipse console will not output this sentence:
I was instantiated.
If you want to apply lazy initialization to all beans, you can set default-lazy-init= "true" at the root node beans, as follows:
<?xml version= "1.0" encoding= "UTF-8"?> <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.xsd " Default-lazy-init= "true" > ...
</beans>
When spring's configuration file--beans.xml the contents of:
<?xml version= "1.0" encoding= "UTF-8"?> <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.xsd ">
<bean id= "Personservice" class= "Cn.itcast.service.impl.PersonServiceBean" scope= "prototype" ></bean >
</beans>
That is, if the bean's scope is prototype, the code for the Springtest class is:
public class Springtest {
@Test public
void Test () {
ApplicationContext ctx = new Classpathxmlapplicationcontext ("Beans.xml"); Instantiate spring Container
}
}
Testing the test () method, you can see that the Eclipse console did not output this sentence:
I was instantiated.
This means that when the bean's scope is prototype, the Bean object is not created when the spring container is started.
However, if you change the code of the Springtest class to:
public class Springtest {
@Test public
void Test () {
ApplicationContext ctx = new Classpathxmlapplicationcontext ("Beans.xml"); Instantiate the Spring container
personservice personservice = (personservice) ctx.getbean ("Personservice");//Get beans from the spring container
}
}
At this point, testing the test () method, you can see that the Eclipse console output this sentence:
I was instantiated.
Confirms that when the bean's scope is prototype, the Bean object is created when the Getbean () method is called. specifying the initialization and destruction methods of the bean
We want to initialize some resources when the bean is initialized. To achieve this, we can modify the code of the Personservicebean class to:
public class Personservicebean implements Personservice {public
void init () {
System.out.println ("Initialization of certain resources"); c9/>} public
Personservicebean () {
System.out.println ("I'm instantiated");
}
@Override public
Void Save () {
System.out.println ("I Am the Save () method");}
}
In this way, our purpose becomes specific: when the Spring container initializes the Personservicebean object, it executes the init () method of the object. In order to achieve this, only the content of the spring configuration file--beans.xml should be modified:
<?xml version= "1.0" encoding= "UTF-8"?> <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.xsd ">
<bean id= "Personservice" class= "Cn.itcast.service.impl.PersonServiceBean" lazy-init= "false"
init-method= "Init"/>
</beans>
If the code for the Springtest class is:
public class Springtest {
@Test public
void Test () {
ApplicationContext ctx = new Classpathxmlapplicationcontext ("Beans.xml"); Instantiate spring Container
}
}
Test () method, the Eclipse console will print:
Now we want to release or close some resources when the bean is destroyed. To achieve this, we can modify the code of the Personservicebean class to:
public class Personservicebean implements Personservice {public
void init () {
System.out.println ("Initialization of certain resources"); c2/>} public
Personservicebean () {
System.out.println ("I'm instantiated");
}
@Override public
Void Save () {
System.out.println ("I Am the Save () method");
}
/**
* When was the bean destroyed? If it is not manually deleted, the bean is always in the spring container, which
means that the bean will be destroyed as the spring container is closed.
*/public
void Destroy () {
System.out.println ("release initialized Resource");}
}
Try to think about the question: When did the Bean object get destroyed? The answer is: if it is not manually removed, the bean is always in the spring container, which means that the bean will be destroyed as the spring container is closed.
Shortly thereafter, we want to modify the contents of spring's configuration file--beans.xml.
<?xml version= "1.0" encoding= "UTF-8"?> <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.xsd ">
<bean id= "Personservice" class= "Cn.itcast.service.impl.PersonServiceBean" lazy-init= "false"
Init-method= "Init" destroy-method= "destroy"/>
</beans>
Finally, we want to modify the code for the test class--springtest.java:
public class Springtest {
@Test public
void Test () {
//ApplicationContext CTX = new Classpathxmlapplicationc Ontext ("Beans.xml"); Instantiate the Spring container
abstractapplicationcontext ctx = new Classpathxmlapplicationcontext ("Beans.xml");
Ctx.close (); Gracefully close the spring container
}
}
At this point, the test () method is tested, and the Eclipse console prints:
This is the life cycle of spring-managed beans. The source can be downloaded by clicking Spring-managed bean Lifecycle .