Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities.
Spring creates all singleton bean objects at application context startup, while the @lazy annotation lazily loads bean objects, which are initialized when they are used.
Therefore, the @lazy annotation can reduce the load time of Spring’s IOC container when it starts, and it can solve the problem of bean loop dependency
1. An introduction to Lazy
The @lazy annotation is used to indicate whether the bean needs Lazy loading.
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Lazy {
/** * Whether lazy initialization should occur. */
boolean value(a) default true;
}
Copy the code
Looking at the annotated source code, you can see that there is only one parameter, which defaults to true, and that the bean object to which the annotation is added is delayed initialization.
2 @ Use of Lazy
The SpringBoot environment is used as an example
1 Prepare a Springboot environment
2 Prepare two entity-class objects
@Data
@NoArgsConstructor
public class User {
private String name;
private String phone;
private Integer age;
private Person person;
public User(String name, String phone, Integer age) {
System.out.println("I User is initialized.............");
this.name = name;
this.phone = phone;
this.age = age; }}Copy the code
@Data
@NoArgsConstructor
public class Person {
private String name;
private String phone;
private Integer age;
private User user;
public Person(String name, String phone, Integer age) {
System.out.println("My Person is initialized.............");
this.name = name;
this.phone = phone;
this.age = age; }}Copy the code
3 Add a startup class
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
public User createUser(a) {
return new User("Han Xuansheng"."11111".24);
}
@Bean
@Lazy
public Person createPerson(a) {
return new Person(Mr.han ""."11111".24); }}Copy the code
4 Test view console
I User is initialized.............Copy the code
5 Remove the @lazy annotation on Person and restart the project
I User is initialized............. My Person is initialized.............Copy the code
3 @ The role of Lazy
1 Lazy-loading bean objects (as shown in the example above)
Solve the problem of loop dependency
1 Add two configuration classes
@Component
public class PersonConfig {
private UserConfig userConfig;
public PersonConfig( UserConfig userConfig) {
this.userConfig = userConfig;
System.out.println("I'm user configuration PersonConfig"); }}Copy the code
@Component
public class UserConfig {
private PersonConfig personConfig;
public UserConfig(PersonConfig personConfig) {
this.personConfig = personConfig;
System.out.println("I am the UserConfig UserConfig"); }}Copy the code
2 restart the project, the project error, there are cyclic dependencies in the code
Description:
The dependencies of some of the beans in the application context form a cycle:
Copy the code
The solution is to add @lazy annotations to one of them, as in
@Component
public class PersonConfig {
private UserConfig userConfig;
public PersonConfig(@Lazy UserConfig userConfig) {
this.userConfig = userConfig;
System.out.println("I'm user configuration PersonConfig"); }}Copy the code
3 Restart the project and view logs
I'm UserConfig PersonConfig. I'm UserConfigCopy the code
4 Error Summary
1 An exception occurs during project startup
'url' attribute is not specified and no embedded datasource could be configu
Solution: The project did not load the application.yml configuration file. Click maven to clean the project and restart the project.