Spring基础 快速入门spring(11) bean scope注解方式

Posted firsttry

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring基础 快速入门spring(11) bean scope注解方式相关的知识,希望对你有一定的参考价值。

在前面我们已经学习过spring中的bean scope, 温故而知新,这次我们将使用注解的方式来验证singleton和prototype的区别。

技术分享图片

bean scope

在spring中,bean的lifecyle大体如下所示

种类详细
singleton (Default) Scopes a single bean definition to a single object instance per Spring IoC container.
prototype Scopes a single bean definition to any number of object instances.
request Scopes a single bean definition to the lifecycle of a single HTTP request; that is, each HTTP request has its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware Spring ApplicationContext.
session Scopes a single bean definition to the lifecycle of an HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext.
globalSession Scopes a single bean definition to the lifecycle of a global HTTP Session. Typically only valid when used in a Portlet context. Only valid in the context of a web-aware Spring ApplicationContext.
application Scopes a single bean definition to the lifecycle of a ServletContext. Only valid in the context of a web-aware Spring ApplicationContext.
websocket Scopes a single bean definition to the lifecycle of a WebSocket. Only valid in the context of a web-aware Spring ApplicationContext.

singleton scope

singleton也是spring的default的scope,使用这种方式的bean的scope至少有如下的特征
|特征|详细
|No.1|使用单态模式,只有一个实例

技术分享图片

我们使用spring的reference的例子来具体说明。如上图所示,虽然在多处被使用,其实他们使用的单态方式提供的同一个实例。

SwimmingService: 提供服务的类(注入对象)

package com.liumiao.demo.spring;

import org.springframework.stereotype.Service;

@Service
public class SwimmingService {
  public SwimmingService(){
    System.out.println("Default SwimmingService Constructor is called.");
  }
  public String providSwimmingCoachingService(){
    return "Swimming coaching Service provided.";
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

Person Interface

package com.liumiao.demo.spring;

public interface Person {
  public String sayhello();
}
  • 1
  • 2
  • 3
  • 4
  • 5

Teacher类

注意此处的注解Scope,使用注解而不是xml配置文件的方式,spring也推荐0配置,应该习惯注解。

package com.liumiao.demo.spring;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

@Service
@Scope("singleton")
public class Teacher implements Person{
  @Autowired
  private SwimmingService swimmingService;

  @Override
  public String sayhello(){
    return "Hello, I am a teacher";
  }

  public String provideSwimmingCoaching(){
    return swimmingService.providSwimmingCoachingService();
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

配置类

package com.liumiao.demo.spring;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("com.liumiao.demo.spring")
public class MyConfig {
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

TestDemo:使用spring

package com.liumiao.demo.spring;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class TestDemo {
  public static void main(String[] args){
    AnnotationConfigApplicationContext context =new AnnotationConfigApplicationContext(MyConfig.class);
    Teacher person = context.getBean(Teacher.class);
    System.out.println(person.provideSwimmingCoaching());
    Teacher personB = context.getBean(Teacher.class);
    if (person == personB){
      System.out.println("person and personB point to the same object...");
    }else{
      System.out.println("person and personB point to the different object...");
    }
    System.out.println("person object address" + person);
    System.out.println("personB object address" + personB);
    context.close();
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

执行结果

Nov 27, 2016 4:20:30 PM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
情報: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@3d646c37: startup date [Thu Nov 27 16:20:30 CST 2016]; root of context hierarchy
Default SwimmingService Constructor is called.
Swimming coaching Service provided.
person and personB point to the same object...
person object addresscom.liumiao.demo.spring.Teacher@4d49af10
personB object addresscom.liumiao.demo.spring.Teacher@4d49af10
Nov 27, 2016 4:20:30 PM org.springframework.context.annotation.AnnotationConfigApplicationContext doClose
情報: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@3d646c37: startup date [Thu Nov 27 16:20:30 CST 2016]; root of context hierarchy
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

prototype确认

prototype方式如下,产生的是不同的实例。

技术分享图片

Teacher类

只需要重新修改Scope的范围,即可确认prototype方式的运行结果

package com.liumiao.demo.spring;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

@Service
@Scope("prototype")
public class Teacher implements Person{
  @Autowired
  private SwimmingService swimmingService;

  @Override
  public String sayhello(){
    return "Hello, I am a teacher";
  }

  public String provideSwimmingCoaching(){
    return swimmingService.providSwimmingCoachingService();
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

执行结果

Nov 27, 2016 4:28:37 PM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
情報: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@3d646c37: startup date [Thu Nov 27 16:28:37 CST 2016]; root of context hierarchy
Default SwimmingService Constructor is called.
Nov 27, 2016 4:28:37 PM org.springframework.context.annotation.AnnotationConfigApplicationContext doClose
情報: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@3d646c37: startup date [Thu Nov 27 16:28:37 CST 2016]; root of context hierarchy
Swimming coaching Service provided.
person and personB point to the different object...
person object addresscom.liumiao.demo.spring.Teacher@4d49af10
personB object addresscom.liumiao.demo.spring.Teacher@279ad2e3
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow



以上是关于Spring基础 快速入门spring(11) bean scope注解方式的主要内容,如果未能解决你的问题,请参考以下文章

Spring基础 快速入门spring boot SPRING INITIALIZR

Spring Boot 2.x 基础教程:快速入门

Spring Boot 2.x基础教程:快速入门

Spring Cloud Spring Boot mybatis分布式微服务云架构快速入门

Spring Cloud Spring Boot mybatis分布式微服务云架构快速入门

Spring Cloud Spring Boot mybatis分布式微服务云架构快速入门