Spring series of tutorials
- Spring Framework Introduction
- Spring Framework module
- Spring development environment to build (Eclipse)
- Create a simple Spring application
- Spring Inversion of Control container (Inversion of Control - IOC)
- Appreciated dependency injection (DI - Dependency Injection)
- Bean XML configuration (1) - through XML configuration loaded Bean
- Bean XML configuration (2) - Bean scope and lifecycle callback method configuration
- Bean XML configuration (3) - configuration dependency injection
- Bean XML configuration (4) - Automatic Assembly
- Bean annotation (Annotation) Configuration (1) - Load Bean via annotations
- Bean annotation (Annotation) Configuration (2) - Bean scope and lifecycle callback method configuration
- Bean Annotations (Annotation) configuration (3) - configuration dependency injection
- Bean Java Configuration
- Spring Aspect Oriented Programming (AOP)
- Spring event (1) - built-in event
- Spring event (2) - Custom Event
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 @Scope
annotations.
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:
- Creation bean instance
- Setting the attribute values of the bean
- Call the initialization callback method
- Use Bean
- Call the destruction callback method
- 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("销毁...");
}
// ...
}