Scope spring with learning 5-bean life cycle

Scope and life cycle

Scope

In the Spring, the body that make up the application by the Spring IoC container managed objects, called bean. Simply put, bean is initialized by the object IoC container, assembly and management.
Using a scope attribute bean tag, the role of the scope is used to specify the scope of the bean
value of four commonly used is a single and a multiple example

Life cycle Explanation
singleton Singleton (default), only one instance of spring ioc bean container, by way of a single example of bean presence
prototype Multi embodiment, each time the bean calls, returns from a new instance of the vessel, i.e. getBean () -> new XxxBean ()
request Acting on the scope of the request web application, every http request will create a new bean, that scope applies only to webApplicationContext environment
sesson Acting on web application session scope, with a httpSession shared a bean, different session uses a different bean, applies only to webApplicationContext environment

user

package com.cong.pojo;

public class User {
    private String name;
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                '}';
    }
}

Singleton

When a scope of the bean to the Singleton, the Spring IoC container exists only a shared bean instances, and all requests for the bean, as long as the id matching that bean definition will return the same instance of the bean. Singleton is a singleton type, that is, when you create it from the container while automatically creates an object of the bean, regardless of whether you use him there, and every time to get the objects are the same object. Note, Singleton scope is the default scope in Spring. To define a singleton in XML bean, may be configured such that:

    <bean id="user" class="com.cong.pojo.User" scope="singleton">
        <property name="name" value="cong"/>
    </bean>

test

    @Test
    public void test1(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        User user = (User) context.getBean("user");
        User user2 = (User) context.getBean("user");
        System.out.println(user == user2);//true
    }

Multi-pattern

When a bean scope of Prototype, showing a plurality of bean definition object instances. Prototype scope bean will result creates a new instance of the bean when (() method which is injected into another bean, or in a container form program calls the getBean) in each of the bean request. Prototype is a prototype type, it does not instantiated when we create the container, but when we get the bean when it will go to create an object, and every time we get to the target is not the same object. According to experience, for stateful bean should use the prototype scope, and to stateless bean should use the singleton scope. Bean defined in XML prototype, may be configured such that:

    <bean id="user2" class="com.cong.pojo.User" scope="prototype">
        <property name="name" value="cong2"/>
    </bean>

test

    @Test
    public void test2(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        User user = (User) context.getBean("user2");
        User user2 = (User) context.getBean("user2");
        System.out.println(user == user2);//false
    }

request

When the scope is a bean Request, it said in a HTTP request, a bean definition to; that is, each will have their own HTTP request bean instances, they create the definition of a single bean. Only valid in the case of Spring ApplicationContext web-based. Consider the following bean definition:

 <bean id="user2" class="com.cong.pojo.User" scope="request"/>

For each HTTP request, Spring container will create loginAction bean definition of a new LoginAction bean instance, and the loginAction bean instance is only valid within the current HTTP request, so you can rest assured that change the internal state of the instance that is created as needed, and other requests in accordance with the definition loginAction bean instance is created, we will not see these changes in the status of a specific request. When the request is finished processing, bean scoped to the request will be destroyed.

session

When a scope of the bean to the Session, showing a HTTP Session, the definition to a bean. Only valid in the case of Spring ApplicationContext web-based. Consider the following bean definition:

 <bean id="user2" class="com.cong.pojo.User" scope="session"/>

For a HTTP Session, Spring container will create a new bean instance userPreferences according to userPreferences bean definitions, and the userPreferences bean is only valid within the current HTTP Session. As with the request scope, you can rest assured that changes the internal state of the instance created as needed, and other HTTP Session according to example userPreferences created will not see these specific changes in a state of HTTP Session. When the HTTP Session is eventually discarded when, bean in the HTTP Session scope will also be discarded.

Life cycle

Singleton object
of Birth: When the container is created when the object is born
alive: as long as the container is still the object has been living
death: the destruction of container, objects demise
summary: the same life cycle and container singleton object
more cases of Object
Birth: When we use the object spring create a framework for us to
live: as long as the objects have been alive during use.
Death: When the object is not a long time, and no other object reference by the Java garbage collected

Singleton

Can be observed by increasing the init () and destroy () method of class user

    public User(){
        System.out.println("对象创建了...");
    }
    //手动添加的对象初始化和销毁方法
    public void init(){
        System.out.println("对象初始化了...");
    }
    public void destroy(){
        System.out.println("对象销毁了...");
    }

test

    @Test
    public void test3(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        User user3 = (User) context.getBean("user3");
        System.out.println(user3.toString());
        //手动关闭容器,因为正常情况下,容器创建之后会一直存在,观察不到user对象的销毁
        //单例模式下,容器关闭,user对象会销毁,但是多例模式下,容器关闭,user对象并不会销毁
        //需要将ApplicationContext改为ClassPathXmlApplicationContext,因为多态
        context.close();
    }

result

对象创建了...
对象初始化了...
User{name='cong'}
对象销毁了...

Multi-pattern

It is difficult to reproduce

Guess you like

Origin www.cnblogs.com/ccoonngg/p/12026754.html