Spring BeanWrapper分析
最近在读DispatcherServlet 源代码,看到父级类org.springframework.web.servlet.HttpServletBean中关于BeanWrapper的一段代码, 继续追看下去,发现
BeanWrapper 是spring 底层核心的JavaBean包装接口, 默认实现类BeanWrapperImpl.所有bean的属性设置都是通过它来实现。
- @Override
- public final void init() throws ServletException {
- if (logger.isDebugEnabled()) {
- logger.debug("Initializing servlet '" + getServletName() + "'");
- }
- // Set bean properties from init parameters.
- try {
- PropertyValues pvs = new ServletConfigPropertyValues(getServletConfig(), this.requiredProperties);
- BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);
- ResourceLoader resourceLoader = new ServletContextResourceLoader(getServletContext());
- bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader, getEnvironment()));
- initBeanWrapper(bw);
- bw.setPropertyValues(pvs, true);
- }
- catch (BeansException ex) {
- logger.error("Failed to set bean properties on servlet '" + getServletName() + "'", ex);
- throw ex;
- }
- // Let subclasses do whatever initialization they like.
- initServletBean();
- if (logger.isDebugEnabled()) {
- logger.debug("Servlet '" + getServletName() + "' configured successfully");
- }
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory类 自动注入工厂抽象类
- @Override
- public Object configureBean(Object existingBean, String beanName) throws BeansException {
- markBeanAsCreated(beanName);
- BeanDefinition mbd = getMergedBeanDefinition(beanName);
- RootBeanDefinition bd = null;
- if (mbd instanceof RootBeanDefinition) {
- RootBeanDefinition rbd = (RootBeanDefinition) mbd;
- bd = (rbd.isPrototype() ? rbd : rbd.cloneBeanDefinition());
- }
- if (!mbd.isPrototype()) {
- if (bd == null) {
- bd = new RootBeanDefinition(mbd);
- }
- bd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
- bd.allowCaching = false;
- }
- <span style="color:#FF0000;"> BeanWrapper bw = new BeanWrapperImpl(existingBean);</span>
- initBeanWrapper(bw);
- populateBean(beanName, bd, bw);
- return initializeBean(beanName, existingBean, bd);
- }
BeanWrapperImpl 继承了属性编辑注册功能
如何设置值 :
- @Override
- public void setPropertyValue(String propertyName, Object value) throws BeansException {
- BeanWrapperImpl nestedBw;
- try {
- //获取嵌套的属性, like map[my.key], 没有嵌套属性就返回自己
- nestedBw = getBeanWrapperForPropertyPath(propertyName);
- }
- catch (NotReadablePropertyException ex) {
- throw new NotWritablePropertyException(getRootClass(), this.nestedPath + propertyName,
- "Nested property in path '" + propertyName + "' does not exist", ex);
- }
- PropertyTokenHolder tokens = getPropertyNameTokens(getFinalPath(nestedBw, propertyName));
- nestedBw.setPropertyValue(tokens, new PropertyValue(propertyName, value));
- }
看下面具体方法的实现
- /**
- * Recursively navigate to return a BeanWrapper for the nested property path.
- * @param propertyPath property property path, which may be nested
- * @return a BeanWrapper for the target bean
- */
- protected BeanWrapperImpl getBeanWrapperForPropertyPath(String propertyPath) {
- int pos = PropertyAccessorUtils.getFirstNestedPropertySeparatorIndex(propertyPath);
- // Handle nested properties recursively.
- if (pos > -1) {
- String nestedProperty = propertyPath.substring(0, pos);
- String nestedPath = propertyPath.substring(pos + 1);
- //递归获取最后一个属性
- BeanWrapperImpl nestedBw = getNestedBeanWrapper(nestedProperty);
- return nestedBw.getBeanWrapperForPropertyPath(nestedPath);
- }
- else {
- return this;
- }
- }
自己实现了个小例子:
- public class HelloWorld {
- private String msg = null;
- private Date date = null;
- public String getMsg() {
- return msg;
- }
- public void setMsg(String msg) {
- this.msg = msg;
- }
- public Date getDate() {
- return date;
- }
- public void setDate(Date date) {
- this.date = date;
- }
- }
- package com.sunkey.test;
- public class Pepole {
- private String name;
- private int sex;
- private HelloWorld helloWorld;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getSex() {
- return sex;
- }
- public void setSex(int sex) {
- this.sex = sex;
- }
- public HelloWorld getHelloWorld() {
- return helloWorld;
- }
- public void setHelloWorld(HelloWorld helloWorld) {
- this.helloWorld = helloWorld;
- }
- }
测试代码:
- @Test
- public void testBeanWapper() throws InstantiationException, IllegalAccessException, ClassNotFoundException {
- Object obj = Class.forName("com.sunkey.test.HelloWorld").newInstance();
- BeanWrapper bw = new BeanWrapperImpl(obj);
- bw.setPropertyValue("msg", "HellowWorld");
- bw.setPropertyValue("date", new Date());
- System.out.println(bw.getPropertyValue("date") + "\n" + bw.getPropertyValue("msg"));
- }
- @Test
- public void testNestedBeanWapper() throws InstantiationException, IllegalAccessException, ClassNotFoundException {
- Object obj = Class.forName("com.sunkey.test.HelloWorld").newInstance();
- BeanWrapper bw = new BeanWrapperImpl(obj);
- bw.setPropertyValue("msg", "HellowWorld");
- bw.setPropertyValue("date", new Date());
- Object objP = Class.forName("com.sunkey.test.Pepole").newInstance();
- BeanWrapper pbw = new BeanWrapperImpl(objP);
- pbw.setPropertyValue("name", "jack");
- pbw.setPropertyValue("helloWorld", obj);
- System.out.println(pbw.getPropertyValue("name") + "\n" + pbw.getPropertyValue("helloWorld.msg"));
- pbw.setPropertyValue("helloWorld.msg", "HellowWorld修改过");
- System.out.println(pbw.getPropertyValue("name") + "\n" + pbw.getPropertyValue("helloWorld.msg")); }
Spring BeanWrapper分析的更多相关文章
- Spring研磨分析、Quartz任务调度、Hibernate深入浅出系列文章笔记汇总
Spring研磨分析.Quartz任务调度.Hibernate深入浅出系列文章笔记汇总 置顶2017年04月27日 10:46:45 阅读数:1213 这系列文章主要是对Spring.Quartz.H ...
- MyBatis整合Spring原理分析
目录 MyBatis整合Spring原理分析 MapperScan的秘密 简单总结 假如不结合Spring框架,我们使用MyBatis时的一个典型使用方式如下: public class UserDa ...
- 深入浅出Spring(四) Spring实例分析
上次的博文中 深入浅出Spring(二) IoC详解 和 深入浅出Spring(三) AOP详解中,我们分别介绍了一下Spring框架的两个核心一个是IoC,一个是AOP.接下来我们来做一个Sprin ...
- 【spring源代码分析】--Bean的解析与注冊
接着上一节继续分析,DefaultBeanDefinitionDocumentReader的parseBeanDefinitions方法: protected void parseBeanDefini ...
- Spring AOP分析(1) -- 基本概念
AOP全称是Aspect Oriented Programming,面向切面编程,是面向对象编程(OOP:Object Oriented Programming)的补充和完善.一般在系统中,OOP利用 ...
- Spring AOP分析(2) -- JdkDynamicAopProxy实现AOP
上文介绍了代理类是由默认AOP代理工厂DefaultAopProxyFactory中createAopProxy方法产生的.如果代理对象是接口类型,则生成JdkDynamicAopProxy代理:否则 ...
- Spring Aop分析
前言 上文讲述ioc框架的实现,本文开始讲述aop.在spring中aop也有3种配置方式,注解形式的我们先不讨论.我们先看看xml形式的配置方式. <aop:config> <ao ...
- Spring IOC分析
前言 关于Spring,我想无需做太多的解释了.每个Java程序猿应该都使用过他.Spring的ioc和aop极大的方便了我们的开发,但是Spring又有着不好的一面,为了符合开闭原则,Spring的 ...
- [置顶] 深入浅出Spring(四) Spring实例分析
上次的博文中 深入浅出Spring(二) IoC详解 和 深入浅出Spring(三) AOP详解中,我们分别介绍了一下Spring框架的两个核心一个是IoC,一个是AOP.接下来我们来做一个Sprin ...
随机推荐
- 使用MD5加密的登陆demo
最近接手了之前的一个项目,在看里面登陆模块的时候,遇到了一堆问题.现在记录下来. 这个登陆模块的逻辑是这样的 1 首先在登陆之前,调用后台的UserLoginAction类的getRandomKey方 ...
- Mapreduce 框架解析
MapReduce过程解析 一.客户端 Map-Reduce的过程首先是由客户端提交一个任务开始的. public static RunningJob runJob(JobConf job) thro ...
- SpriteBuilder中子节点的相对位置(%百分比定位)
子节点(或在这里确切的为精灵sprites)50%的偏移效果使得其在父节点中居中显示,该父节点的纹理在左下角(锚点为0,0). 这样做好过用父节点的位置的实际值来定位.根据父节点实际位置来定位在早期的 ...
- Gradle 1.12用户指南翻译——第二十六章. War 插件
其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Github上的地址: https://g ...
- Xcode 下删除Provisioning Profiles文件
Xcode 中有很多不可以用的Provisioning Profiles 文件,每次选择手机证书时,看着那么长的菜单很烦有木有? 在Xcode 5中删除 Provisioning Profiles,打 ...
- SharePoint 门户网站的图片轮播-页面定制
这个想法是自己突然的一个想法,想想我们经常用SharePoint做门户网站,不知道你们多数项目都是怎么完成的,我们客户要求的效果都还是很严格的,所有展现起来,还是很漂亮的,但是很多时候的效果,还是难以 ...
- The 14th tip of DB Query Analyzer
The 14th tip of DB Query Analyzer Ma Genfeng (Guangdong Unitoll Services incorporated, Guangzhou 5 ...
- pyqt5 动画在QThread线程中无法运行问题
自己做了一个tcp工具,在学习动画的时候踩了坑,需求是根据上线变绿色,离线变灰色,如果连接断开了,则变为灰色 问题现象: 可以看到点击"连接","离线"的时候动 ...
- 每天几分钟跟小猫学前端之node系列:用node实现最简单的爬虫
先来段求分小视频: https://www.iesdouyin.com/share/video/6550631947750608142/?region=CN&mid=6550632036246 ...
- minimun depth of binary tree
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...