Spring @Scope注解+Singleton Scope +@Component示例

x33g5p2x  于2022-02-24 转载在 Spring  
字(5.7k)|赞(0)|评价(0)|浏览(1328)

在这篇文章中,我们将讨论如何使用@Scope注解来创建一个范围为单子的bean。

我们使用@Scope来定义@Component类或@Bean定义的范围。它可以是单例、原型、请求、会话、globalSession或一些自定义的范围。在这篇文章中,我们将通过一个例子来讨论单子的作用域。
当Spring Bean的作用域为单子时,Spring IoC容器就会为该Bean定义的对象创建一个确切的实例。

默认情况下,Spring IoC容器会将所有Bean创建并初始化为单例。但是我们可以使用元素的scope="singleton"属性或使用@Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON)注解将bean的范围定义为单子。
我们将使用基于注解(@Component)和基于Java的配置(@Bean)来演示这个例子。

Spring @Scope注解+Singleton Scope +@Component示例

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

使用的工具和技术

  • 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-dependson-annotation</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <name>spring-scope-example</name>
  9. <url>http://maven.apache.org</url>
  10. <properties>
  11. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  12. </properties>
  13. <dependencies>
  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>

创建MessageService接口,如下。

MessageService.java

  1. package net.javaguides.spring.scope;
  2. public interface MessageService {
  3. String getMessage();
  4. void setMessage(String message);
  5. }

让我们创建TwitterMessageService类,实现MessageService接口。

TwitterMessageService.java

  1. package net.javaguides.spring.scope;
  2. import org.springframework.beans.factory.config.ConfigurableBeanFactory;
  3. import org.springframework.context.annotation.Scope;
  4. import org.springframework.stereotype.Component;
  5. @Component
  6. @Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON)
  7. public class TwitterMessageService implements MessageService {
  8. private String message;
  9. @Override
  10. public String getMessage() {
  11. return message;
  12. }
  13. @Override
  14. public void setMessage(String message) {
  15. this.message = message;
  16. }
  17. }

基于注解的配置 - AppConfig.java

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

  1. package net.javaguides.spring.scope;
  2. import org.springframework.context.annotation.ComponentScan;
  3. import org.springframework.context.annotation.Configuration;
  4. @Configuration
  5. @ComponentScan(basePackages = "net.javaguides.spring")
  6. public class AppConfig {
  7. }

@ComponentScan注解扫描@Component注解的类在basePackages属性指定的包中的所有bean。

运行中的Spring应用程序 - Application.java

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

  1. package net.javaguides.spring.scope;
  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. MessageService messageService = context.getBean(MessageService.class);
  7. messageService.setMessage("TwitterMessageService Implementation");
  8. System.out.println(messageService.getMessage());
  9. MessageService messageService1 = context.getBean(MessageService.class);
  10. System.out.println(messageService1.getMessage());
  11. context.close();
  12. }
  13. }

输出

  1. TwitterMessageService Implementation
  2. TwitterMessageService Implementation

让我们使用基于Java的配置与@Bean注解来开发同一个例子。

Spring @Scope 注解 + Singleton Scope + @Bean 示例

创建MessageService接口,如下所示。

MessageService.java

  1. package net.javaguides.spring.scope;
  2. public interface MessageService {
  3. String getMessage();
  4. void setMessage(String message);
  5. }

让我们创建TwitterMessageService类,实现MessageService接口。

TwitterMessageService.java

  1. package net.javaguides.spring.scope;
  2. import org.springframework.beans.factory.config.ConfigurableBeanFactory;
  3. import org.springframework.context.annotation.Scope;
  4. import org.springframework.stereotype.Component;
  5. public class TwitterMessageService implements MessageService {
  6. private String message;
  7. @Override
  8. public String getMessage() {
  9. return message;
  10. }
  11. @Override
  12. public void setMessage(String message) {
  13. this.message = message;
  14. }
  15. }

基于注解的配置 - AppConfig.java

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

  1. package net.javaguides.spring.scope;
  2. import org.springframework.beans.factory.config.ConfigurableBeanFactory;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.context.annotation.Scope;
  6. @Configuration
  7. public class AppConfig {
  8. @Bean
  9. @Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON)
  10. public MessageService messageService() {
  11. return new TwitterMessageService();
  12. }
  13. }

@ComponentScan注解扫描所有其类被@Component注解在由basePackages属性指定的包中的bean。

运行中的Spring应用程序 - Application.java

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

  1. package net.javaguides.spring.scope;
  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. MessageService messageService = context.getBean(MessageService.class);
  7. messageService.setMessage("TwitterMessageService Implementation");
  8. System.out.println(messageService.getMessage());
  9. MessageService messageService1 = context.getBean(MessageService.class);
  10. System.out.println(messageService1.getMessage());
  11. context.close();
  12. }
  13. }

输出

  1. TwitterMessageService Implementation
  2. TwitterMessageService Implementation

本文的源代码可在我的GitHub仓库https://github.com/RameshMF/spring-core-tutorial找到。

相关文章