Spring Annotations of @Scope

@Scope

@Scope is under org.springframework.context.annotation annotation packet, it refers to the scope Ioc container management objects, there is substantially scope (singleton, prototype), custom scope, the Web Scope (reqeust, session, globalsession ).

  1. singleton: Singleton, by definition, only one bean container instance object.
  2. prototype: prototype model, each container creates a new instance of the object when using the Bean.
  3. request: For each HTTP request using the request Bean will have defined a new instance, i.e. each HTTP request will produce different Bean instance. Only in the Web application using Spring, the scope to be effective.
  4. session: For each HTTP Session, Bean defined using the session will produce a new instance. Similarly only use Spring in a Web application, the scope to be effective.
  5. globalsession: each global HTTP Session, Bean defined using the session will produce a new instance. Typically, the only effective when using the portlet context. Similarly only use Spring in a Web application, the scope to be effective.

Note
singleton scope is to develop Spring annotation default, and examples will be singleton container tracking and maintenance of its life cycle, but after the prototype example of a container you've created, and not tracking and management.
Therefore singleton instances will be reused, the cost of the system will be relatively small, prototype on the contrary, but still have to be in accordance with their needs.

Prove the following

package com.springboot.gyh.ch2.scope;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

@Service
@Scope("prototype")
public class DemoPrototypeService {

}
package com.springboot.gyh.ch2.scope;

import org.springframework.stereotype.Service ;

//默认的是scope("singleton")
@Service
public class DemoSingletonService {

}
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

//配置类
@Configuration
@ComponentScan("com.springboot.gyh.ch2")
public class ScopeConfig {
}

package com.springboot.gyh.ch2.scope;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ScopeConfig.class) ;

        DemoSingletonService s1 = context.getBean(DemoSingletonService.class) ;
        DemoSingletonService s2 = context.getBean(DemoSingletonService.class) ;

        DemoPrototypeService s3 = context.getBean(DemoPrototypeService.class) ;
        DemoPrototypeService s4 = context.getBean(DemoPrototypeService.class) ;

        System.out.println("s1是否s2相等:"+s1.equals(s2));
        //true
        System.out.println("s3是否s4相等:"+s3.equals(s4));
		//false
		
        context.close();

    }
}

Results are as follows:
Here Insert Picture Description

Published 56 original articles · won praise 3 · Views 1182

Guess you like

Origin blog.csdn.net/qq_40788718/article/details/103337657