1, bean scopes
1, create a new maven project, add the following dependence
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.0.5.RELEASE</version> </dependency>
2, a new class Person entity
package com.yefengyu.annotation.bean; public class Person { private String name; private Integer age; public Person() { } public Person(String name, Integer age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
3, a new configuration class
package com.yefengyu.annotation.config; import com.yefengyu.annotation.bean.Person; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class MainConfig { @Bean public Person person() { return new Person("张三", 20); } }
4, the test
public static void main(String[] args) { ApplicationContext ctx= new AnnotationConfigApplicationContext(MainConfig.class); Person person1 = (Person)ctx.getBean("person"); Person person2 = (Person)ctx.getBean("person"); System.out.println(person1 == person2); //true }
The above results are seen many times get a bean from the container, are actually the same object, this is due to the scope of the bean is caused by a single instance.
prototype : multi-instance: ioc container does not start to call the method to create objects in the container. Every time you get a call the method will create an object;
Singleton : ioc container calls the start method to create objects into ioc container: single instance (the default value). After each acquisition is to take directly from the container,
Request: with a request to create an instance of
session: create an instance of the same session
In addition, we can modify the configuration class, increase print, easy to observe:
@Configuration public class MainConfig { @Bean public Person person() { System.out.println("创建Person对象"); return new Person("张三", 20); } }
We re-run the above test code to create a Person object phrase found only print once.
5, use: Scope Notes
@Configuration public class MainConfig { @Bean @Scope("prototype") public Person person() { System.out.println("创建Person对象"); return new Person("张三", 20); } }
Note : Scope annotation scope of the bean is modified, you can modify the scope of the bean can be used together with the notes and @Bean @Component series of notes.
We re-run the above test code found objects to create Person sentence printed twice. And the two objects are not the same, comparison using == returns false.
Scope values may be used in the string above, the prototype for example, may be used
- ConfigurableBeanFactory.SCOPE_PROTOTYPE
- ConfigurableBeanFactory.SCOPE_SINGLETON
- WebApplicationContext.SCOPE_REQUEST
- WebApplicationContext.SCOPE_SESSION
2, lazy loading
Specifically for real single class, also known as lazy loading.
@Configuration public class MainConfig { @Bean public Person person() { System.out.println("创建Person对象"); return new Person("张三", 20); } }
Single Instance bean: the object is created by default when the vessel started;
AnnotationConfigApplicationContext ctx =new AnnotationConfigApplicationContext(MainConf.class);
The following code does not use this test to the person, but we can see through the print has been loaded the Person object.
public static void main(String[] args) { ApplicationContext ctx= new AnnotationConfigApplicationContext(MainConfig.class); String[] names = ctx.getBeanDefinitionNames(); for (String name : names) { System.out.println(name); } }
创建Person对象
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalRequiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
mainConfig
person
Now we add a comment @Lazy
@Configuration public class MainConfig { @Bean @Lazy public Person person() { System.out.println("创建Person对象"); return new Person("张三", 20); } }
In this case the test again, the results are as follows
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalRequiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
mainConfig
person
Description In the case of delayed loading, if not used to the bean, then it will not really create an object in a container (not printed: Create a Person object ), and now we get to add person objects in the test code.
public static void main(String[] args) { ApplicationContext ctx= new AnnotationConfigApplicationContext(MainConfig.class); String[] names = ctx.getBeanDefinitionNames(); for (String name : names) { System.out.println(name); } Person person= (Person)ctx.getBean("person"); }
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalRequiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
mainConfig
person
创建Person对象
Lazy loading summarized: Start container does not create the object. First use (get) to create objects when Bean, and initialize