您的位置:首页 > 编程语言 > Java开发

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

2016-12-02 07:20 447 查看
在前面我们已经学习过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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息