Spring Starter (V): Spring in the scope of the bean

1. Spring bean in various scopes

By default, Spring application context bean all are created in the form of a single embodiment (Singleton), i.e., regardless of a given bean bean is injected into how many other times the same instance are injected .

Spring defines a variety of scopes, bean can be created based on these scopes:

  1. Singleton (Singleton): throughout the application, only create an instance of the bean.
  2. Prototype (Prototype): each injection or when acquired by the Spring application context, creates a new bean instance.
  3. Session (Session): In the Web application, create a bean instance for each session.
  4. Request (Request): In the Web application, create a bean instance for each request.

Singleton is the default scope, if you want to create using other bean scope, use the @Scopenotes, the notes can be @Component, @Service, @Beanused in conjunction and other annotations.

2. Example

For a better understanding, we have to understand the difference between the lower and Singleton ProtoType through specific code examples.

2.1 New type of bean Singleton

package chapter03.scope;

import org.springframework.stereotype.Service;

@Service
public class DemoSingletonService {
}

Scope Spring because the default configuration is Singleton, so the above code is equivalent to the following code:

package chapter03.scope;

import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

@Service
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
public class DemoSingletonService {
}

The above code can also be written @Scope("singleton").

2.2 ProtoType new type of bean

If automatic assembly bean, the syntax is:

package chapter03.scope;

import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

@Service
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class DemoPrototypeService {
}

The above code can also be written @Scope("prototype").

If it is declared in the Java bean class configuration, the syntax is:

@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public DemoPrototypeService demoPrototypeService() {
    return new DemoPrototypeService();
}

If it is declared in the xml bean, the syntax is:

<bean id="demoPrototypeService" class="chapter03.scope.DemoPrototypeService"
      scope="prototype"/>

New configuration class 2.3

package chapter03.scope;

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

@Configuration
@ComponentScan("chapter03.scope")
public class ScopeConfig {
}

2.4 Test

Create a Main class, were acquired from secondary Bean Spring vessel main () method, and then determine whether the same Bean Examples:

package chapter03.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 p1 = context.getBean(DemoPrototypeService.class);
        DemoPrototypeService p2 = context.getBean(DemoPrototypeService.class);

        System.out.println("s1 与 s2 是否相等:" + s1.equals(s2));
        System.out.println("p1 与 p2 是否相等:" + p1.equals(p2));

        context.close();
    }
}

Results are as follows:

It can also be seen from the results of operation, namely default type of Singleton Bean, no matter how many calls will only create an instance.

3. Notes

Singleton type of Bean, @ Scope annotation value must be: singleton.

ProtoType type of Bean, @ Scope annotation value must be: prototype.

Even capitalization inconsistency does not work, as we will DemoPrototypeService class code amended as follows:

package chapter03.scope;

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

@Service
@Scope("PROTOTYPE")
public class DemoPrototypeService {
}

In this case run the code, it will error:

4. The reference source and

Source Address: https://github.com/zwwhnly/spring-action.git , welcome to download.

Wang Yunfei "Java EE developers insurgents: Spring Boot real"

The original is not easy, if that article can learn something, like a welcome point, a commentary on, off a note, this is my greatest motivation insist on writing.

If you are interested, please add my micro letter: zwwhnly , waiting for you to talk technology, workplace, work and other topics (PS: I am a programmer struggle in Shanghai).

Published 34 original articles · won praise 107 · views 20000 +

Guess you like

Origin blog.csdn.net/zwwhnly/article/details/100150480