Spring @Lazy 注解示例

x33g5p2x  于2022-10-06 转载在 Spring  
字(3.2k)|赞(0)|评价(0)|浏览(505)

在这篇快速文章中,我们将通过一个例子来讨论Spring的@Lazy注解。

默认情况下,Spring IoC容器会在应用程序启动时创建并初始化所有单体Bean。我们可以通过使用@Lazy注解来阻止单体Bean的预初始化。
@Lazy注解可以用在任何直接或间接用@Component注解的类或用@Bean注解的方法上。在这个例子中,我们将使用一个基于Java的配置(使用@Configuration和@Bean)。

Spring @Lazy 注解示例

让我们创建一个例子来演示在Spring应用程序中使用@Lazy注解的用法。

使用的工具和技术

  • Spring Framework - 5.1.0.RELEASE
  • JDK - 8或更高版本
  • Maven - 3.2以上
  • IDE - Eclipse Mars/STS

创建一个简单的Maven项目

使用你喜欢的IDE创建一个简单的Maven项目,关于打包结构请参考下面的章节。如果你是maven新手,请阅读本文《如何创建一个简单的Maven项目》。

项目结构

下图显示了项目结构,供您参考。

the pom.xml File

<project xmlns="http://maven.apache.org/POM/4.0.0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>net.javaguides.spring</groupId>
 <artifactId>spring-lazy-annotation</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <url>http://maven.apache.org</url>

 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 </properties>

 <dependencies>
  <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-context</artifactId>
   <version>5.1.0.RELEASE</version>
  </dependency>

 </dependencies>
 <build>
  <sourceDirectory>src/main/java</sourceDirectory>
  <plugins>
   <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.5.1</version>
    <configuration>
     <source>1.8</source>
     <target>1.8</target>
    </configuration>
   </plugin>
  </plugins>
 </build>
</project>

创建Spring Bean - FirstBean和SecondBean

FirstBean.java

package net.javaguides.spring.lazy;

public class FirstBean {

    public FirstBean() {
        System.out.println("Inside FirstBean Constuctor");
    }

    public void test() {
        System.out.println("Method of FirstBean Class");
    }
}

SecondBean.java

package net.javaguides.spring.lazy;

public class SecondBean {

    public SecondBean() {
        System.out.println("Inside SecondBean Constuctor");
    }

    public void test() {
        System.out.println("Method of SecondBean Class");
    }
}

基于Java的配置 - AppConfig.java

在基于java的配置类中声明上述Bean。

package net.javaguides.spring.lazy;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;

@Configuration
public class AppConfig {

    @Lazy(value = true)
    @Bean
    public FirstBean firstBean() {
        return new FirstBean();
    }

    @Bean
    public SecondBean secondBean() {
        return new SecondBean();
    }
}

运行Spring应用程序 - Application.java

让我们创建一个主类并运行一个应用程序。

package net.javaguides.spring.lazy;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Application {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
        FirstBean firstBean = context.getBean(FirstBean.class);
        firstBean.test();
        context.close();
    }
}

输出

Inside SecondBean Constuctor
Inside FirstBean Constuctor
Method of FirstBean Class

我们可以看到,bean secondBean是由Spring容器初始化的,而bean firstBean是显式初始化的。本文的源代码可以在我的GitHub仓库https://github.com/RameshMF/spring-core-tutorial中找到。

相关文章