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

原文地址:https://www.cnblogs.com/firsttry/p/10294303.html

时间: 2024-08-27 03:16:44

Spring基础 快速入门spring(11) bean scope注解方式的相关文章

Spring基础 快速入门spring(1) 基础概念

作为流行了10年以上的老将,spring依然精神矍铄,影响不减.本文将对spring很基础的概念进行介绍以及为学习spring最核心和基础的知识作环境搭建的准备. Spring官网 http://docs.spring.io/ 简介 Spring为JAVA企业级应用提供了一个较为复杂框架流行框架.spring到底能做什么,或者说spring现在能做什么? 除了spring framework之外还有spring boot/spring data/spring cloud/- 快接近无所不包了.已

Spring基础 快速入门spring boot(2) SPRING INITIALIZR

SPRING INITIALIZR是spring boot在构建项目时候非常有效, 虽然说Maven以及Spring boot提供的starter使用起来非常简单, 但是由于组件和关联部分众多,有这样一个可视化的配置构建管理工具仍然很不错. 在这篇文章中我们将会在IntelliJ IDEA中利用SPRING INITIALIZR来创建一个Hello World的例子程序, 不包括Maven下载的速度的话, 1分钟应该够了. 创建一个SPRING INITIALIZR项目 使用IntelliJ I

Spring的快速入门第一天

文档版本 开发工具 测试平台 工程名字 日期 作者 备注 V1.0 2016.06.21 lutianfei none 本文内容 Spring框架的概述 Spring的快速入门 Spring 工厂接口 在MyEclipse 配置Spring的xml文件提示 IoC容器装配Bean(xml配置方式) Ioc容器装配Bean(注解方式) 在web项目中集成Spring Spring 整合 junit4 测试 Spring框架学习路线 Spring的Ioc Spring的AOP , AspectJ S

微服务的入门级微框架Spring Boot快速入门

详情请交流  QQ  709639943 00.微服务的入门级微框架Spring Boot快速入门 00.基于java的微信公众号二次开发视频教程 00.leetcode 算法 面试 00.北风网 零基础到数据(大数据)分析专家-首席分析师 00.快速上手JMeter 00.Jmeter 00.2017年Java web开发工程师成长之路 00.R语言速成实战 00.R语言数据分析实战 00.Python+Django+Ansible Playbook自动化运维项目实战 00.Java深入微服务

Spring基础系列8 -- Spring自动装配bean

Spring基础系列8 -- Spring自动装配bean 转载:http://www.cnblogs.com/leiOOlei/p/3548290.html 1.      Auto-Wiring ‘no’ 2.      Auto-Wiring ‘byName’ 3.      Auto-Wiring ‘byType 4.      Auto-Wiring ‘constructor’ 5.      Auto-Wiring ‘autodetect’ Spring Auto-Wiring Be

Spring MVC 快速入门

Spring MVC 快速入门 环境准备一个称手的文本编辑器(例如Vim.Emacs.Sublime Text)或者IDE(Eclipse.Idea Intellij) Java环境(JDK 1.7或以上版本) Maven 3.0+(Eclipse和Idea IntelliJ内置,如果使用IDE并且不使用命令行工具可以不安装) 一个最简单的Web应用 使用Spring Boot框架可以大大加速Web应用的开发过程,首先在Maven项目依赖中引入spring-boot-starter-web:po

Spring Boot快速入门(二):http请求

原文地址:https://lierabbit.cn/articles/4 一.准备 postman:一个接口测试工具 创建一个新工程 选择web 不会的请看Spring Boot快速入门(一):Hello Spring Boot 二.开始 新建java类RequestCtrl 1.添加一个all方法,使用@RequestMapping注解,可以处理所有的http请求 @RestController//这是一个控制器并只返回数据不寻找视图 public class RequestCtrl { @R

使用 Spring Boot 快速构建 Spring 框架应用--转

原文地址:https://www.ibm.com/developerworks/cn/java/j-lo-spring-boot/ Spring 框架对于很多 Java 开发人员来说都不陌生.自从 2002 年发布以来,Spring 框架已经成为企业应用开发领域非常流行的基础框架.有大量的企业应用基于 Spring 框架来开发.Spring 框架包含几十个不同的子项目,涵盖应用开发的不同方面.如此多的子项目和组件,一方面方便了开发人员的使用,另外一个方面也带来了使用方面的问题.每个子项目都有一定

使用 Spring Boot 快速构建 Spring 框架应用,PropertyPlaceholderConfigurer

Spring 框架对于很多 Java 开发人员来说都不陌生.自从 2002 年发布以来,Spring 框架已经成为企业应用开发领域非常流行的基础框架.有大量的企业应用基于 Spring 框架来开发.Spring 框架包含几十个不同的子项目,涵盖应用开发的不同方面.如此多的子项目和组件,一方面方便了开发人员的使用,另外一个方面也带来了使用方面的问题.每个子项目都有一定的学习曲线.开发人员需要了解这些子项目和组件的具体细节,才能知道如何把这些子项目整合起来形成一个完整的解决方案.在如何使用这些组件上