8 -- 深入使用Spring -- 2...2 指定Bean的作用域
8.2.2 指定Bean的作用域
当使用XML 配置方式来配置Bean实例时,可以通过scope来指定Bean实例的作用域,没有指定scope属性的Bean实例作用域默认是singleton。
当采用零配置方式来管理Bean实例时,可使用@Scope Annotation,只要在该Annotation中提供作用域的名称即可。
package edu.pri.lime._8_2_2.bean.impl; import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component; import edu.pri.lime._8_2_2.bean.Axe; @Scope("prototype")
@Component
public class SteelAxe implements Axe{ public String chop() {
return null;
} }
在一些极端的情况下,如果不想使用基于Annotation的方式来指定作用域,而是希望提供自定义的作用域解析器,让自定义的解析器实现ScopeMetadataResolver接口,并提供自定义的作用域解析策略,然后在配置扫描器时指定解析器的全限定类名即可。
Class : MyScopeMetadataResolver
package edu.pri.lime._8_2_2.bean.impl; import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.ScopeMetadata;
import org.springframework.context.annotation.ScopeMetadataResolver; public class MyScopeMetadataResolver implements ScopeMetadataResolver { public ScopeMetadata resolveScopeMetadata(BeanDefinition definition) {
return null;
} }
XML :
<?xml version="1.0" encoding="UTF-8"?>
<!-- Spring 配置文件的根元素,使用Spring-beans-4.0.xsd语义约束 -->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 自动扫描指定包及其子包下的所有Bean类。 -->
<context:component-scan base-package="edu.pri.lime._8_2_1.bean" scope-resolver="edu.pri.lime._8_2_2.bean.impl.MyScopeMetadataResolver" /> </beans>
啦啦啦
8 -- 深入使用Spring -- 2...2 指定Bean的作用域的更多相关文章
- 六、Spring之DI的Bean的作用域
Spring提供“singleton”和“prototype”两种基本作用域,另外提供“request”.“session”.“global session”三种web作用域:Spring还允许用户定 ...
- java:Spring框架2(bean的作用域,静态工厂和实例工厂,自动装配,动态代理)
1.bean的作用域,静态工厂和实例工厂: bean.xml: <?xml version="1.0" encoding="UTF-8"?> < ...
- Spring系列8:bean的作用域
本文内容 bean定义信息的意义 介绍6种bean的作用域 bean定义信息的意义 Spring中区分下类.类定义信息,类实例对象的概念?不容易理解,以餐馆中点炒饭为例. 类: 相当于你看到菜单上炒饭 ...
- Spring框架系列(三)--Bean的作用域和生命周期
Bean的作用域 Spring应用中,对象实例都是在Container中,负责创建.装配.配置和管理生命周期(new到finalize()) Spring Container分为两种: 1.BeanF ...
- Spring系列四:Bean Scopes作用域
等闲识得东风面,万紫千红总是春. 概述 在Spring框架中,我们可以在六个内置的spring bean作用域中创建bean,还可以定义bean范围.在这六个范围中,只有在使用支持Web的applic ...
- spring注解开发:bean的作用域与懒加载
1.bean的作用域 1.新建一个maven工程,添加如下依赖 <dependency> <groupId>org.springframework</groupId> ...
- Spring学习记录(五)---bean的作用域scope
作用域:singleton:单例,整个应用中只创建一个实例(默认) prototype:原型,每次注入时都新建一个实例 session:会话,每个会话创建一个实例 request:请求,每个请求创建一 ...
- JavaEE开发之Spring中Bean的作用域、Init和Destroy方法以及Spring-EL表达式
上篇博客我们聊了<JavaEE开发之Spring中的依赖注入以及AOP>,本篇博客我们就来聊一下Spring框架中的Bean的作用域以及Bean的Init和Destroy方法,然后在聊一下 ...
- spring容器bean的作用域 & spring容器是否是单例的一些问题
Spring容器中Bean的作用域 当通过Spring容器创建一个Bean实例时,不仅可以完成Bean实例的实例化,还可以为Bean指定特定的作用域.Spring支持如下5种作用域: singleto ...
随机推荐
- 史上最全 Css Hack汇总
前言 每次要找个东西都得慢慢去翻自己收集的一些东西,每次都是那么花时间,再加上有时存放时间久远就忘了当时是存在哪了,为了方便查询及阅读,决定把一些Css Hack 收集起来... 1.区别不同浏览器, ...
- 超级简单却不知道:html标签的嵌套规则
XHTML的标签有许多:div.ul.li.dl.dt.dd.h1~h6.p.a.addressa.span. strong……我们在运用这些标签搭建页面结构的时候,是可以将它们无限嵌套的,但是,嵌套 ...
- mysql 5.7.10 启动多实例笔记
1. 复制配置文件 cp /etc/my.cnf /etc/my3308.cnf 2. 修改配置文件 3. 创建目录, 并赋予权限 4. 初始化数据库 ---> 有报错 2018-01-03T0 ...
- 【转】25个非常实用的jQuery/CSS3应用组件
今天分享25款功能十分强大的jQuery/CSS3应用插件,欢迎收藏. 1.jQuery水晶样式下拉导航 这是一款非常不错的jQuery多功能下拉菜单插件,菜单外观呈水晶样式,晶莹剔透,功能丰富,包含 ...
- 【HTML】div居中显示
方法1: .parent { width:800px; height:500px; border:2px solid #000; position:relative; } .child { width ...
- JsonCpp 判断 value 中是否有某个KEY
JsonCpp如何判断是否有某个KEY,使用json[“key”]和isXXX的函数即可. 如果json中没有key键,则会创建一个空成员或者返回一个空成员. bool isNull() const; ...
- Winform控件学习笔记【第三天】——ListBox
1. 属性事件列表: SelectionMode 组件中条目的选择类型,即多选(Multiple).单选(Single) Rows 列表框中显示总共多少行 Selecte ...
- ibatis中的安全问题
http://blog.csdn.net/yangqillohe/article/details/4139265
- tRNAscan-SE 预测tRNA基因
tRNAscan-SE 软件可以根据输入的基因组序列,预测对应的tRNA的基因 在线的tRNAscan-SE的链接如下:http://lowelab.ucsc.edu/tRNAscan-SE/ 如下图 ...
- Win7-64位安装TensorFlow-GPU
1.查看电脑显卡 路径:计算机--属性--设备管理器-显示适配器 2.显卡(GPU)是否支持CUDN https://developer.nvidia.com/cuda-gpus 3.安装 1)CUD ...