Spring @Lazy 注解示例

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

在这篇快速文章中,我们将通过一个例子来讨论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

  1. <project xmlns="http://maven.apache.org/POM/4.0.0"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>net.javaguides.spring</groupId>
  6. <artifactId>spring-lazy-annotation</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <url>http://maven.apache.org</url>
  9. <properties>
  10. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  11. </properties>
  12. <dependencies>
  13. <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
  14. <dependency>
  15. <groupId>org.springframework</groupId>
  16. <artifactId>spring-context</artifactId>
  17. <version>5.1.0.RELEASE</version>
  18. </dependency>
  19. </dependencies>
  20. <build>
  21. <sourceDirectory>src/main/java</sourceDirectory>
  22. <plugins>
  23. <plugin>
  24. <artifactId>maven-compiler-plugin</artifactId>
  25. <version>3.5.1</version>
  26. <configuration>
  27. <source>1.8</source>
  28. <target>1.8</target>
  29. </configuration>
  30. </plugin>
  31. </plugins>
  32. </build>
  33. </project>

创建Spring Bean - FirstBean和SecondBean

FirstBean.java

  1. package net.javaguides.spring.lazy;
  2. public class FirstBean {
  3. public FirstBean() {
  4. System.out.println("Inside FirstBean Constuctor");
  5. }
  6. public void test() {
  7. System.out.println("Method of FirstBean Class");
  8. }
  9. }

SecondBean.java

  1. package net.javaguides.spring.lazy;
  2. public class SecondBean {
  3. public SecondBean() {
  4. System.out.println("Inside SecondBean Constuctor");
  5. }
  6. public void test() {
  7. System.out.println("Method of SecondBean Class");
  8. }
  9. }

基于Java的配置 - AppConfig.java

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

  1. package net.javaguides.spring.lazy;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.context.annotation.Lazy;
  5. @Configuration
  6. public class AppConfig {
  7. @Lazy(value = true)
  8. @Bean
  9. public FirstBean firstBean() {
  10. return new FirstBean();
  11. }
  12. @Bean
  13. public SecondBean secondBean() {
  14. return new SecondBean();
  15. }
  16. }

运行Spring应用程序 - Application.java

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

  1. package net.javaguides.spring.lazy;
  2. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  3. public class Application {
  4. public static void main(String[] args) {
  5. AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
  6. FirstBean firstBean = context.getBean(FirstBean.class);
  7. firstBean.test();
  8. context.close();
  9. }
  10. }

输出

  1. Inside SecondBean Constuctor
  2. Inside FirstBean Constuctor
  3. Method of FirstBean Class

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

相关文章