- 浏览: 332869 次
- 性别:
- 来自: 北京
-
文章分类
最新评论
-
longge424:
你好,可否将saml与spring security结合使用的 ...
SAML介绍 -
bnmnba:
mark
(转)Ant 学习笔记(一) -
redhacker:
写的还不错!谢谢!
SAML介绍 -
linluxian:
SAML介绍 -
hyj1254:
有效,good.
Linux创建目录与删除目录命令具体分析
目前在开发的过程中遇到:通过spring在后台对前台form 传递过来的属性自动封装到对应的bean中,对其中的一些类进行了相应的学习,还是有些不是很清楚,将以下文章转载分享:
看spring源码的时间也有很长一段时间了,对其中bean的注入也有一定的了解。总想对这一段时间的学习经历做出总结,又不知道从何处开始。也看了从主要脉络开始写,本人也看了计文柯老师编写的《spring技术内幕》,觉得将的很生动,就是对于一个刚学习的人来说有点深奥。想从一些基础的东西开始慢慢的解释spring的bean内核,也给希望给那些和我一样想了解spring内核的人提供一些帮助。也希望各位老鸟看了之后提出宝贵的建议。
先从spring的bean的属性操作开始吧。
一 Bean的properties的操作特性
1 properties操作的整体的结构图
这是我整理出来的bean相关属性的操作接口图。从这个图中我们可以清晰的看出对于bean属性相关操作的功能。在最上面的PropertyAccessor接口提供了对于bean属性的取值,赋值等基本的操作,并且定义了属性常用的分割符“.”,“[”,“]”等。TypeConverter接口提供了在需要的情况下对一些类型的值,进行转换和封装。这些实际性的操作在我们给bean的赋值的时候都会用到。例如 在我们的引用中,我们有一个属性start是int类型的,在spring的配置文件中
给这个属性赋21的值。我们在这设置的是一个字符串类型的值,在bean的初始化的时候就需要同过实现convertIfNecessary的方法将字符型串类型转化成int类型。
PropertyEditorRegistry接口提供了一些对属性编辑器注册和查找的方法。而这些方法的具体实现是在PropertyEditorRegistrySupport类中实现的。ConfigurablePropertyAccessor接口只是继承了上面的的三个接口。方便在后面实现类去做实现的时候只用去实现这个统一接口。同时也增加了在对属性赋值的时候是否去提取他以前值的标志接口. BeanWrapper接口继承了ConfigurablePropertyAccessor接口,同时还添加了一些bean的类型,实例添加获取等操作,从而完成了一个bean对象的初步的服装。
下面对这些接口的实现类做主要的分析。
u PropertyEditorRegistrySupport
PropertyEditorRegistrySupport的类实现了各种类型的的属性的编译器.
这个类里面定义几种类型的类型编辑器的存储容器,是否启用一些通用属性的编辑器。
通过getDefaultEditor的方法来获取默认类型的属性的编辑器.在defaultEditors容器中存储的是属性的类型,各种类型的编辑器的。
通过doRegisterDefaultEditors来注册了64中默认类型的编辑器。这些编辑器都是我们在编码过程中常见的类型,例如cahr,Class,String,List等等。
private void doRegisterDefaultEditors() {
this.defaultEditors = new HashMap(64);
this.defaultEditors.put(Charset.class, new CharsetEditor());
this.defaultEditors.put(Class.class, new ClassEditor());
this.defaultEditors.put(Class[].class, new ClassArrayEditor());
this.defaultEditors.put(File.class, new FileEditor());
this.defaultEditors.put(InputStream.class, new InputStreamEditor());
this.defaultEditors.put(Locale.class, new LocaleEditor());
this.defaultEditors.put(Pattern.class, new PatternEditor());
this.defaultEditors.put(Properties.class, new PropertiesEditor());
this.defaultEditors.put(Resource[].class, new ResourceArrayPropertyEditor());
this.defaultEditors.put(URI.class, new URIEditor());
this.defaultEditors.put(URL.class, new URLEditor());
// Default instances of collection editors.
// Can be overridden by registering custom instances of those as custom editors.
this.defaultEditors.put(Collection.class, new CustomCollectionEditor(Collection.class));
this.defaultEditors.put(Set.class, new CustomCollectionEditor(Set.class));
this.defaultEditors.put(SortedSet.class, new CustomCollectionEditor(SortedSet.class));
this.defaultEditors.put(List.class, new CustomCollectionEditor(List.class));
this.defaultEditors.put(SortedMap.class, new CustomMapEditor(SortedMap.class));
// Default editors for primitive arrays.
this.defaultEditors.put(byte[].class, new ByteArrayPropertyEditor());
this.defaultEditors.put(char[].class, new CharArrayPropertyEditor());
// The JDK does not contain a default editor for char!
this.defaultEditors.put(char.class, new CharacterEditor(false));
this.defaultEditors.put(Character.class, new CharacterEditor(true));
// Spring's CustomBooleanEditor accepts more flag values than the JDK's default editor.
this.defaultEditors.put(boolean.class, new CustomBooleanEditor(false));
this.defaultEditors.put(Boolean.class, new CustomBooleanEditor(true));
// The JDK does not contain default editors for number wrapper types!
// Override JDK primitive number editors with our own CustomNumberEditor.
this.defaultEditors.put(byte.class, new CustomNumberEditor(Byte.class, false));
this.defaultEditors.put(Byte.class, new CustomNumberEditor(Byte.class, true));
this.defaultEditors.put(short.class, new CustomNumberEditor(Short.class, false));
this.defaultEditors.put(Short.class, new CustomNumberEditor(Short.class, true));
this.defaultEditors.put(int.class, new CustomNumberEditor(Integer.class, false));
this.defaultEditors.put(Integer.class, new CustomNumberEditor(Integer.class, true));
this.defaultEditors.put(long.class, new CustomNumberEditor(Long.class, false));
this.defaultEditors.put(Long.class, new CustomNumberEditor(Long.class, true));
this.defaultEditors.put(float.class, new CustomNumberEditor(Float.class, false));
this.defaultEditors.put(Float.class, new CustomNumberEditor(Float.class, true));
this.defaultEditors.put(double.class, new CustomNumberEditor(Double.class, false));
this.defaultEditors.put(Double.class, new CustomNumberEditor(Double.class, true));
this.defaultEditors.put(BigDecimal.class, new CustomNumberEditor(BigDecimal.class, true));
this.defaultEditors.put(BigInteger.class, new CustomNumberEditor(BigInteger.class, true));
// Only register config value editors if explicitly requested.
if (this.configValueEditorsActive) {
StringArrayPropertyEditor sae = new StringArrayPropertyEditor();
this.defaultEditors.put(String[].class, sae);
this.defaultEditors.put(short[].class, sae);
this.defaultEditors.put(int[].class, sae);
this.defaultEditors.put(long[].class, sae);
}
}
/**
* Copy the default editors registered in this instance to the given target registry.
* @param target the target registry to copy to
*/
protected void copyDefaultEditorsTo(PropertyEditorRegistrySupport target) {
target.defaultEditors = this.defaultEditors;
target.defaultEditorsActive = this.defaultEditorsActive;
target.configValueEditorsActive = this.configValueEditorsActive;
}
注册一个需要用到的属性类型编辑器
这个方法需要两个参数,属性的类型和能对这个类型做出处理的属性编辑器。他是通过
下面的方法的来实现主体注册的
注册共享的属性编辑器
通过属性的路径或者属性的类型来获取属性的编辑器
通过属性的类型来获取属性的编辑器。
u BeanWrapperImpl
BeanWrapperImpl类是对bean的封装类的BeanWrapper的实现,主要是设置bean的属性值,获取bean的类型的等操作。
下面是实例化的BeanWrapperImpl,注册默认的属性编辑器,调用的是从
PropertyEditorRegistrySupport中集成过来的。
获取到某个属性的PropertyDescriptor,
主要是通过调用getPropertyDescriptorInternal的方法来实现,这个方法是获取到当前对象的Class类的所有属性的PropertyDescriptor缓存容器,从容器中查找当前的名称的属性的PropertyDescriptor。
在上面的代码中当propertyName是普通属性的的名字(不包含.)返回的是当前对象this。
通过getPropertyType方法可以获取到指定属性名称的类型。
下面是获取到具体属性的值
我来具体的看下是怎么通过属性名称来获取拥有当前属性的实例对象.
先判断属性名称中有没有包含.当没有包含的时候直接返回当前对象,当包含.的时候实例化属性名称中包含的类.
发表评论
-
(转)Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)
2015-06-04 16:49 1852互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已 ... -
maven+springMVC+mybatis+junit详细搭建过程(转)
2015-06-01 09:46 3602目录[-] springMVC+mybatis框 ... -
linux awk的使用详解(收藏)
2014-09-03 16:04 712简介 awk是一个强大的文本分析工具,相对于grep的查 ... -
OpenSessionInViewFilter详解(转)
2013-07-08 14:25 1469OpenSessionInViewFilter是Spring ... -
【转】web后台线程中获取spring容器内的bean
2013-05-16 14:59 1924有时候需要启动一个后台守护线程,做一些别的事情。这时候怎么获 ... -
JSP中文乱码的产生原因及解决方案-转
2013-05-15 17:33 2416JSP中文乱码的产生原因及解决方案在JSP的开发过程中,经常 ... -
SAML介绍
2013-03-25 21:03 4428工作了一段时间,接触到不少知识,好久没有进行总 ... -
linux下通过JNI用C/C++中调用JAVA类
2012-07-20 16:15 1791最近工作中完成 ... -
逆向工程中的问题:Hibernate project not selected 解决方式(转)
2012-03-14 10:43 2198今天用myeclipse自动生成POJO类时一直找不到java ... -
责任链模式(转)
2011-11-22 21:38 1095责任链模式是一种对象的行为模式。 在责任链模 ... -
Java序列化与反序列化
2011-11-08 23:38 580基本概念:序列化是将对象状态转换为可保持或传输的格式的过程。与 ... -
Spring Aspectj的使用
2011-11-03 15:25 1348Applicationcontext.xml代码 ... -
JS中的原型方法prototype
2011-11-01 11:03 1054JS中的phototype是JS中比较难理解的一个部分 ... -
java动态代理--JDK
2011-10-30 18:04 946JAVA的动态代理 代理模式 代理模式是常用的java ... -
Hibernate更新某些字段的几种update方法
2011-09-20 22:48 1779Hibernate 中如果直接使用 Session.up ... -
Hibernate的merge与update方法的区别
2011-09-20 22:47 985今天做了个测试,写了个测试用例来看看merge与update时 ... -
Hibernate学习:Transient、Persistent、Detached三个状态
2011-09-19 22:15 1453Transient、Persistent、Detached是H ... -
Hibernate中get和load方法的区别
2011-09-19 22:10 1030load加载方法: Users user = ... -
hibernate主键生成策略
2011-09-13 21:52 1100先来看看主键映射的标签: < ... -
勉励继续写技术博客
2011-09-03 15:50 869最近一直就没怎么写博客,由于各种原因吧,客观的还有主观的 ...
相关推荐
spring spring 11boot application properties配置详解.docx application properties配置详解.docxspring boot application properties配置详解.docx
spring spring 11boot application properties配置详解.pdfapplication properties配置详解.pdfspring boot application properties配置详解.pdf
《Spring框架5.2.8.RELEASE源码详解》 Spring框架是Java开发中的核心组件,它以其模块化、灵活性和强大的功能深受开发者喜爱。5.2.8.RELEASE是Spring框架的一个稳定版本,提供了诸多改进和新特性,旨在提升性能、...
《Spring Framework 4.3.25.RELEASE 官方源码详解》 Spring Framework作为Java领域中的核心框架,以其强大的功能和灵活的设计理念,深受广大开发者喜爱。本篇文章将聚焦于Spring Framework 4.3.25.RELEASE的官方...
在深入学习和分析Spring源码时,有时我们需要一个完整的运行环境来理解其内部机制。"spring-cglib-repack-3.2.5.jar"、"spring-objenesis-repack-2.6.jar"和"spring-objenesis-repack-2.5.1.jar"这三个文件是Spring...
Spring 源码分析 Spring 框架是 Java 语言中最流行的开源框架之一,它提供了一个强大且灵活的基础设施来构建企业级应用程序。在 Spring 框架中,IOC 容器扮演着核心角色,本文将深入分析 Spring 源码,了解 IOC ...
《Spring Framework 5.2.3源码深度解析》 Spring Framework是Java开发中的核心框架,它为构建高质量的应用提供了全面的基础设施。5.2.3版本是Spring的一个稳定版本,包含了众多改进和新特性。这个官方原版源码包为...
spring 3.2.4 Realease 的所有jar包: spring-context-3.2.4.RELEASE.jar spring-core-3.2.4.RELEASE.jar spring-beans-3.2.4.RELEASE.jar spring-test-3.2.4.RELEASE.jar spring-web-3.2.4.RELEASE.jar spring-aop-...
在Spring框架的开发和调试过程中,我们经常需要深入源码以理解其工作原理或解决特定问题。然而,直接编译Spring源码时,可能会遇到依赖缺失的问题。在本案例中,"Spring源码编译缺少的两个包:spring-cglib-repack-...
此压缩文件中包含spring的如下组件的doc,source,jar: spring-aop-4.1.6.RELEASE.jar spring-aspects-4.1.6.RELEASE.jar spring-beans-4.1.6.RELEASE.jar spring-context-4.1.6.RELEASE.jar spring-context-...
在Spring框架的高版本源码分析过程中,可能会遇到缺少特定库的情况,比如"spring-cglib-repack-3.2.4.jar"和"spring-objenesis-repack-2.4.jar"这两个文件。这些文件是Spring框架运行时的重要组成部分,对于理解和...
通过以上分析,我们可以看到Spring Framework 4.2.5.RELEASE的强大之处,它为开发者提供了丰富的功能和灵活的扩展性。深入研究其源码,不仅有助于我们更好地使用Spring,还能提升我们的编程技艺和系统设计能力。
基于Spring Cloud的网约车项目源码.zip基于Spring Cloud的网约车项目源码.zip基于Spring Cloud的网约车项目源码.zip基于Spring Cloud的网约车项目源码.zip基于Spring Cloud的网约车项目源码.zip基于Spring Cloud的网...
Spring框架,作为Java领域最为广泛应用的开源框架之一,以其强大的功能和灵活的设计理念深受开发者喜爱。4.3.2.RELEASE是Spring的一个稳定版本,它包含了许多重要的改进和优化,深入理解其源码对于提升开发技能和...
最新版源码 spring-framework-5.2.17.RELEASE.zip最新版源码 spring-framework-5.2.17.RELEASE.zip
最新版源码 spring-framework-5.2.18.RELEASE.zip最新版源码 spring-framework-5.2.18.RELEASE.zip
本文将围绕Spring Framework的核心特性,通过源码分析,帮助开发者深化对Spring的理解,提升开发技能。 1. **核心模块**:Spring Framework由多个模块组成,如Core Container(核心容器)、Data Access/Integration...
Spring 源码深度剖析Spring 源码深度剖析Spring 源码深度剖析Spring 源码深度剖析Spring 源码深度剖析Spring 源码深度剖析Spring 源码深度剖析Spring 源码深度剖析Spring 源码深度剖析Spring 源码深度剖析Spring ...
Spring 源码深度剖析Spring 源码深度剖析Spring 源码深度剖析Spring 源码深度剖析Spring 源码深度剖析Spring 源码深度剖析Spring 源码深度剖析Spring 源码深度剖析Spring 源码深度剖析Spring 源码深度剖析Spring ...
Spring 源码深度剖析Spring 源码深度剖析Spring 源码深度剖析Spring 源码深度剖析Spring 源码深度剖析Spring 源码深度剖析Spring 源码深度剖析Spring 源码深度剖析Spring 源码深度剖析Spring 源码深度剖析Spring ...