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.
prototypeScopes a single bean definition to any number of object instances.
requestScopes 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.
sessionScopes a single bean definition to the lifecycle of an HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext.
globalSessionScopes 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.
applicationScopes a single bean definition to the lifecycle of a ServletContext. Only valid in the context of a web-aware Spring ApplicationContext.
websocketScopes 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.";
  }
}

Person Interface

package com.liumiao.demo.spring;

public interface Person {
  public String sayhello();
}

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();
  }
}

配置类

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 {
}

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();
  }
}

执行结果

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

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();
  }
}

执行结果

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值