Bean annotation (Annotation) Configuration (2) - Bean scope and lifecycle callback method configuration


Spring series of tutorials


In Bean XML configuration (2) - Bean scope and lifecycle callbacks configuration , we introduce the use of XML configuration Bean scope and lifecycle callbacks. This article describes the use of notes arranged Bean scope and lifecycle callbacks.

Bean scopes

When you create a bean spring, you can specify the scope. A scope has the following five types:

  • Singleton (Singleton)
    default scope, a container only one example of Bean spring.

  • Prototype (the prototype)
    each time a new instance Get Bean generated.

  • Request (request)
    Scope http request is single, only one instance of a single http request the Bean. Once the request is completed, bean instance is destroyed.

  • Session (session)
    scope is a single session, a single session only one instance of the Bean. Once the session ends, bean instance is destroyed.

  • Global session (global-session)
    in Portlet application, only one instance of each global session of Bean. Ordinary Servlet applications indistinguishable session scope.

Bean scopes provided with notes, you can use @Scopeannotations.

Example:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
@Scope("prototype") // 设置作用域
public class App {

    // ...
}

Use annotations to specify initialization callback method and destroy method callbacks

Bean Spring container is responsible for managing the life cycle, as shown below:

  1. Creation bean instance
  2. Setting the attribute values ​​of the bean
  3. Call the initialization callback method
  4. Use Bean
  5. Call the destruction callback method
  6. Destruction Bean

Bean When you create, you need to perform some resources (databases, sockets, file) applications such as initialization, can handle the initialization callback method in Bean, this method is called by the Spring container.

Also destroyed in the Bean, need to perform some resource destruction (database, sockets, file) applications, etc., can be processed in Bean's destruction callback method, this method is called by the Spring container.

Initialization callback method and destroy method callbacks can be specified by the following comment:

  • @PostConstruct - This annotation specifies the initialization callback method
  • @PreDestroy - This annotation specifies the callback method to destroy

Example:

package com.qikegu.demo;
 
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;


public class App {

    // ...
    
    @PostConstruct
    public void init() {
        System.out.println("初始化...");
    }
    
    @PreDestroy
    public void close() {
        System.out.println("销毁...");
    }
    
    // ...
}

Guess you like

Origin www.cnblogs.com/haibianren/p/11652219.html
Recommended