代码改变世界

Spring.NET学习笔记(4)-对象作用域和类型转换

  Clingingboy  阅读(889)  评论(0编辑  收藏  举报


一.作用域

作为对象定有生命周期,singleton和prototype是最基本的实例状态,其他三个则是对于web平台而言的。熟悉asp.net的则一看便清楚了.


作用域 描述

singleton

在每个Spring IoC容器中一个bean定义对应一个对象实例。

prototype

一个bean定义对应多个对象实例。

request

在一次HTTP请求中,一个bean定义对应一个实例;即每次HTTP请求将会有各自的bean实例, 它们依据某个bean定义创建而成。该作用域仅在基于web的Spring ApplicationContext情形下有效。

session

在一个HTTP Session中,一个bean定义对应一个实例。该作用域仅在基于web的Spring ApplicationContext情形下有效。

application

在一个全局的HTTP Session中,一个bean定义对应一个实例。典型情况下,仅在使用portlet context的时候有效。该作用域仅在基于web的Spring ApplicationContext情形下有效。


以下两幅图很好的表达了singleton和prototype的概念
image

image


二.类型转换器


类型转换器是.net已经有的概念,其实类型转换器的概念在.net中用的很广,asp.net,wpf则到处是类型转换器,只不过平时大家很少用,因为基本的类型.net都帮你做了内置转换。以下是内置spring支持的类型转换器,当然也可以自己扩展,扩展的配置文件需要通过配置来扩展。

Type Explanation
RuntimeTypeConverter Parses strings representing System.Types to actual System.Types and the other way around.
FileInfoConverter Capable of resolving strings to a System.IO.FileInfo object.
StringArrayConverter Capable of resolving a comma-delimited list of strings to a string-array and vice versa.
UriConverter Capable of resolving a string representation of a Uri to an actual Uri-object.
CredentialConverter Capable of resolving a string representation of a credential for Web client authentication into an instance of System.Net.ICredentials
StreamConverter Capable of resolving Spring IResource Uri (string) to its corresponding InputStream-object.
ResourceConverter Capable of resolving Spring IResource Uri (string) to an IResource object.
ResourceManagerConverter Capable of resolving a two part string (resource name, assembly name) to a System.Resources.ResourceManager object.
RgbColorConverter Capable of resolving a comma separated list of Red, Green, Blue integer values to a System.Drawing.Color structure.
ExpressionConverter Capable of resolving a string into an instance of an object that implements the IExpression interface.
NameValueConverter Capable of resolving an XML formatted string to a Specialized.NameValueCollection
RegexConverter Capable of resolving a string into an instance of Regex
RegistryKeyConverter Capable of resolving a string into a Microsoft.Win32.RegistryKey object.


这部分先了解,用到再看

编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示