Spring基础:快速入门spring(11):bean scope注解方式
2016-12-02 07:20
447 查看
在前面我们已经学习过spring中的bean scope, 温故而知新,这次我们将使用注解的方式来验证singleton和prototype的区别。
|特征|详细
|No.1|使用单态模式,只有一个实例
我们使用spring的reference的例子来具体说明。如上图所示,虽然在多处被使用,其实他们使用的单态方式提供的同一个实例。
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."; } }
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
相关文章推荐
- spring笔记11---通过注解的方式配置bean
- JAVAWEB开发之Spring详解之——Spring的入门以及IOC容器装配Bean(xml和注解的方式)、Spring整合web开发、整合Junit4测试
- Spring基础:快速入门spring(6):properties文件方式的值注入
- Spring基础:快速入门spring boot(1):CLI方式的Helloworld
- Spring基础:快速入门spring(7):bean的lifecyle
- Spring基础:快速入门spring(9):注解注入
- servlet调用spring中的bean(注解方式)
- webservice快速入门-使用JAX-WS注解的方式快速搭建ws服务端和客户端(一)
- Spring 中 MongDB java Bean 的映射配置(注解方式)
- webservice快速入门-使用JAX-WS注解的方式快速搭建ws服务端和客户端(一)
- Spring自动注入,利用注解实现spring基本配置详解,Spring注解快速入门
- spring实战四之Bean的自动装配(注解方式)
- Spring中bean三种注解方式。
- Spring整理11 -- 面对切面(AOP)1 -- 注解配置的方式实现AOP
- Spring学习(九)使用ioc注解方式配置bean
- spring 注解方式配置Bean
- spring实战二之Bean的自动装配(非注解方式)
- webservice快速入门-使用JAX-WS注解的方式快速搭建ws服务端和客户端(一)
- servlet调用spring容器中的bean,的两种方式一种注解一种xml配置
- spring配置文件中bean标签中init-method和destroy-method和用注解方式配置