Three, Spring assembly disposed scope of @Scope

Or the same as the previous section and, first of all look at the configuration class: MainConfig2

@Configuration
名
public class MainConfig2 {

    @Scope("singleton") // 默认就是单实例的
    @Bean("person")
    public Person person(){
        System.out.println("给容器中添加Person....");
        return new Person("张三", 25);
    }
}   

Look at room person entity classes:

public class Person {

    private String name;
    private Integer age;
    private String nickName;
    // 省略...
}

We know that in the spring bean default is a single instance, so @Scope("singleton")all the same, we write a test class to test

    @Test
    public void test02(){
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig2.class);
        String[] definitionNames = applicationContext.getBeanDefinitionNames();
        for (String name : definitionNames) {
            System.out.println(name);
        }
        
    //  System.out.println("ioc容器创建完成....");
    //  Object bean = applicationContext.getBean("person");
    //  Object bean2 = applicationContext.getBean("person");
    //  System.out.println(bean == bean2);
    }

Print this:

给容器中添加Person.... // 容器在一启动的时候便创建好了bean
mainConfig2 // 配置类本身也是bean
person

These results can be seen:

A container when they start to create better bean ,

We then comment open

    @Test
    public void test02(){
        System.out.println("ioc容器创建完成....");
        Object bean = applicationContext.getBean("person");
        Object bean2 = applicationContext.getBean("person");
        System.out.println(bean == bean2);
    }

Print this:

给容器中添加Person....
ioc容器创建完成....
true

Can be drawn, the result is true, explained taken twice bean is the same, proven default is a single instance,


Other remain unchanged, will @Scope("singleton")replace@Scope("prototype")

Dir perform a test method:

mainConfig2 
person

Found that container does not create, indicating prototypelower scope, spring is not the bean container with the start and created

Performs two two test methods again:

    @Test
    public void test02(){
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig2.class);
        String[] definitionNames = applicationContext.getBeanDefinitionNames();
        for (String name : definitionNames) {
            System.out.println(name);
        }
        
        System.out.println("ioc容器创建完成....");
        Object bean = applicationContext.getBean("person");
        Object bean2 = applicationContext.getBean("person");
        System.out.println(bean == bean2);
    }

Print Results:

mainConfig2 
person
ioc容器创建完成....
给容器中添加Person....
给容器中添加Person....
false

Observations Description: Get the bean when we come to dynamically create a bean, and the bean does not equal two, also shows that this time is more than bean instance, naturally not equal.


In fact, in addition spring bean scope than these two,

spring official document described as follows: the first two commonly used, followed by several almost irrelevant

@Scope:调整作用域
prototype:多实例的:ioc容器启动并不会去调用方法创建对象放在容器中。
                    每次获取的时候才会调用方法创建对象;
singleton:单实例的(默认值):ioc容器启动会调用方法创建对象放到ioc容器中。
            以后每次获取就是直接从容器(map.get())中拿,
request:同一次请求创建一个实例
session:同一个session创建一个实例

There is also need to mention is, that if you want a single instance, ioc container is not loaded at the time of creation, but only load the first time to use the bean, you can use @Lazy notes, plus created in the configuration class the bean's method, after the addition of this comment, for single-instance, only loads the first time the use of bean bean of
fuel, good night!

Guess you like

Origin www.cnblogs.com/heliusKing/p/11361221.html