首页 > 其他分享 >mini spring learning

mini spring learning

时间:2023-04-01 14:33:19浏览次数:45  
标签:mini String spring Object printStackTrace learning pe catch public

image
https://www.pexels.com/zh-cn/photo/768089/

http://www.implements.fun:8080/tag/minispring

package com.minis.beans.factory;

import com.minis.beans.BeansException;

public interface BeanFactory {
    Object getBean(String name) throws BeansException;
	boolean containsBean(String name);
	boolean isSingleton(String name);
	boolean isPrototype(String name);
	Class<?> getType(String name);

}
package com.minis.beans;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class BeanWrapperImpl extends AbstractPropertyAccessor {
	Object wrappedObject;
	Class<?> clz;
	
	public BeanWrapperImpl(Object object) {
		super();	
		this.wrappedObject = object;
		this.clz = object.getClass();
	}

	@Override
	public void setPropertyValue(PropertyValue pv) {
		BeanPropertyHandler propertyHandler = new BeanPropertyHandler(pv.getName());
		PropertyEditor pe = this.getCustomEditor(propertyHandler.getPropertyClz());
		if (pe == null) {
			pe = this.getDefaultEditor(propertyHandler.getPropertyClz());
			
		}
		if (pe != null) {
			pe.setAsText((String) pv.getValue());
			propertyHandler.setValue(pe.getValue());
		}
		else {
			propertyHandler.setValue(pv.getValue());			
		}

	}

	class BeanPropertyHandler {
		Method writeMethod = null;
		Method readMethod = null;
		Class<?> propertyClz = null;
		
		public Class<?> getPropertyClz() {
			return propertyClz;
		}

		public BeanPropertyHandler(String propertyName) {
			try {
				Field field = clz.getDeclaredField(propertyName);
				propertyClz = field.getType();
				this.writeMethod = clz.getDeclaredMethod("set"+propertyName.substring(0,1).toUpperCase()+propertyName.substring(1),propertyClz);
				this.readMethod = clz.getDeclaredMethod("get"+propertyName.substring(0,1).toUpperCase()+propertyName.substring(1));
			} catch (NoSuchMethodException e) {
				e.printStackTrace();
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (NoSuchFieldException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
		}
		
		public Object getValue() {
			Object result = null;
			writeMethod.setAccessible(true);
			
			try {
				result =  readMethod.invoke(wrappedObject);
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
			return result;

		}

		public void setValue(Object value) {
				writeMethod.setAccessible(true);
				try {
					writeMethod.invoke(wrappedObject, value);
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					e.printStackTrace();
				}
		}

	}

}

标签:mini,String,spring,Object,printStackTrace,learning,pe,catch,public
From: https://www.cnblogs.com/ukzq/p/17278582.html

相关文章

  • springboot和redis执行lua脚本——踩坑
    问题:原先想使用redis执行lua脚本作为项目限流基础,lua脚本后写完后执行一直报错如下图:  卡了几天了,没看明白咋回事,一次偶然试了一下解决了,传递lua参数需要时String类型难怪说报错强转String类型异常  灵感来源参考文章:踩坑之RedisTemplate执行Lua脚本-知乎(zhihu.c......
  • 基于Spring的AOP(注解方式)
    面向切面编程:基于Spring的AOP(注解方式)1-配置:pom文件:<packaging>jar</packaging><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactI......
  • Spring(Bean详解)
    GoF之工厂模式GoF是指二十三种设计模式GoF23种设计模式可分为三大类:创建型(5个):解决对象创建问题。单例模式工厂方法模式抽象工厂模式建造者模式原型模式结构型(7个):一些类或对象组合在一起的经典结构。代理模式装饰模式适配器模式组......
  • 从原理上理解Spring如何解决循环依赖
    上图展示了循环依赖是什么,类A存在B类的成员变量,所以类A依赖于类B,类B同样存在类A的成员变量,所以类B也依赖于类A,就形成了循环依赖问题。Spring是如何创建Bean的Spring中Bean初始化的精简流程如下:简要描述一下SpringBean的创建流程:(1)首先Spring容器启动之后,会根据使用不同类型......
  • Springboot 系列 (26) - Springboot+HBase 大数据存储(四)| Springboot 项目通过 HBase
    ApacheHBase是Java语言编写的一款Apache开源的NoSQL型数据库,不支持SQL,不支持事务,不支持Join操作,没有表关系。ApacheHBase构建在ApacheHadoop和ApacheZookeeper之上。ApacheHBase:https://hbase.apache.org/HBase的安装配置,请参考“Springboot系列(24)-......
  • About Interviews and Learning------Learning journals 5
     Thisweek,weproducedagroupassignment,aninterviewvideooncultural appropriation andappreciation,fromwhichwecanalwayslearnsomethinguseful。Ifthereisanythingtolearnfromit,Ithinkitisstillabouthowtoexpressandorgani......
  • 项目一众筹网07_01_SpringSecurity框架简介和用法、SpringSecurity负责的是 权限验证
    项目一众筹网07_01_SpringSecurity文章目录项目一众筹网07_01_SpringSecurity01简介SpringSecurity负责的是权限验证02-SpringSecurity简介03-Spring的注解模式maven引入Spring环境04-准备测试环境05-加入SpringSecurity环境06-实验1-放行首页和静态资源(下一篇)01简介现在主流的权......
  • 项目一众筹网09_00_SpringSecurity
    系列文章目录提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加例如:第一章Python机器学习入门之pandas的使用提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档文章目录系列文章目录前言一、pandas是什么?二、使用步骤1.引入库2.读入数据总结前言提示:这......
  • SpringMVC 中常用注解
    1、控制器类的注解(1)@Controller作用:修饰类,一个类被它修饰,就成了控制器类,负责接收和处理HTTP请求,可以返回页面和数据;(2)@RestController(@Controller+@ResponseBody的组合注解)作用:修饰类,一个类被它修饰,就成了控制器类,只返回给用户数据,默认将返回的对象数据转换为jso......
  • SpringMVC 框架的介绍
    Java早期的MVC模型主要使用Servlet组件。用户的请求首先到达Servlet,Servlet作为控制器接收请求,然后调度JavaBean读写数据库的数据,最后将结果放到jsp中展现给用户。但是,Servlet组件功能有限,而且与jsp的耦合度过高,使得基于Servlet组件的MVC架构开发很不方便......