Spring Getting Started Tutorial (ii) Life cycle and scope

Source: Internet
Author: User


The following knowledge points are for use in the configuration file (annotation the corresponding label later in this article will elaborate)


Bean's life cycle:

Why care about this problem?

The purpose of our IOC is to allow the spring IOC to help us manage objects so that we do not need to manually go to new, when new, how to manage dependencies between objects, when to destroy, and so on. The object in the spring container seems to be a bean, of course, to understand the bean's life cycle.

What are the life cycle processes?

Define, initialize, use, destroy.


Initialization: (1) Implement the Org.springframework.beans.factory.InitlialzingBean interface and cover the Afterpropertiesset method;

(2) configuration Init-method;

Destroy: (1) Implement Org.springframework.beans.factory.DisposableBean interface, and cover Destory method;

(2) configuration Destroy-method;

Of course, you can also use the Configure default mode ( see spirng the corresponding configuration file below ):

Default-init-method= "Defautinit" default-destroy-method= "Defaultdestroy


Import Org.springframework.beans.factory.disposablebean;import org.springframework.beans.factory.initializingbean;//in practical applications, it is important to note that the following three methods of initialization and destruction only need to choose one, inheriting the interface of the kind does not need to be declared in the configuration file, While the other two need to be declared in the configuration file//Here is the implementation of the corresponding interface, but note to overwrite the corresponding method public class Beanlifecycle implements Initializingbean, Disposablebean {// Default-init-method= default construction method in "Defautinit" default-destroy-method= "Defaultdestroy" configuration file public void Defautinit () { System.out.println ("Bean defautinit.");} Default-init-method= the default construction method in the "Defautinit" default-destroy-method= "Defaultdestroy" configuration file public void Defaultdestroy ( {System.out.println ("Bean Defaultdestroy.");} @Override//Overwrite interface required method public void Destroy () throws Exception {System.out.println ("Bean destroy.");} @Override//Overwrite interface required method public void Afterpropertiesset () throws Exception {System.out.println ("Bean afterpropertiesset.");} Alternatively, declare the corresponding method in the init-method= "start" destroy-method= "stop" corresponding class in the configuration file public void Start () {System.out.println ("Bean Start. "); Another method, specified in the configuration file, is init-method= "Start" Destroy-metHod= "Stop" corresponds to a class in which the corresponding method is declared public void Stop () {System.out.println ("Bean stop.");}} 



There are several relationships to note here (1) Whether you are using the default initialization and destruction methods, inheriting interfaces, or declaring them in a configuration file, you need to write the appropriate method in the corresponding class . Of course, using the default settings for more general classes is flexible, so you can rewrite the appropriate methods in different files, rather than configuring them every time, or implementing interfaces. (2) If we use three methods at the same time, then default-init-method= "Defautinit" default-destroy-method= "Defaultdestroy" does not work. (3) in the specific class can not implement the default specific method (just do not error, but do not initialize, destroy the role), but, if it is inherited interface or own configuration, then in the specific class must implement the corresponding method;




Scope of the Bean:

Why care about this problem?

As with the life cycle above, we have to be concerned about where beans can be used, and whether different HTTP requests are created with the same bean, which is directly related to our development.

There are several types of bean scopes:

Single: Singleton, a bean container exists only one copy;

Prototype: Each request creates a new instance;

Request: Create an instance each time the HTTP request takes effect only within the current request;

Session: Creates an instance of each HTTP request and only takes effect within the current session;


<?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 ">                <!-- A Web project can have a lot of spring configuration files, but these files have to read the configuration file themselves when they need to load the object              scope= "Singleton, note this configuration--        <bean id=" Beanscope "Class=" Com.xidian.BeanScope "scope=" singleton "></bean> </beans>


Obviously, the different scope configuration, the corresponding use effect is certainly different, depending on the development requirements.



Did you see it? Welcome to discuss http://blog.csdn.net/code_7/



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Spring Getting Started Tutorial (ii) Life cycle and scope

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.