反射 XMLUtil
package com.dys.util; import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.XPath;
import org.dom4j.io.SAXReader;
import com.dys.annotation.DysAnnotation;
import com.dys.model.BeanDefinition;
import com.dys.model.PropertyDefinition; public class XMLUtils {
private List<BeanDefinition> beanDefinitions = new ArrayList<BeanDefinition>();
private Map<String, Object> singletons = new HashMap<String, Object>();
public XMLUtils(String fileName) {
this.readXML(fileName);
this.initilizeBeans();
this.annotationInject();
this.injectObject();
}
private void annotationInject() {
try {
for(String beanName : singletons.keySet()) {
Object bean = singletons.get(beanName);
if(bean != null) {
PropertyDescriptor[] propertyDescriptors = Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors();
for(PropertyDescriptor propertyDescriptor : propertyDescriptors) {
Method setMethod = propertyDescriptor.getWriteMethod();
if(setMethod != null && setMethod.isAnnotationPresent(DysAnnotation.class)) {
DysAnnotation dysA = setMethod.getAnnotation(DysAnnotation.class);
Object beanValue = null;
if(dysA.name() != null && !"".equals(dysA.name())) {
String name = dysA.name();
beanValue = singletons.get(name); } else {
String nam = propertyDescriptor.getName();
beanValue = singletons.get(nam);
if(beanValue == null) {
for(String key : singletons.keySet()) {
if(propertyDescriptor.getPropertyType().isAssignableFrom(singletons.get(key).getClass())) {
beanValue = singletons.get(key);
break;
}
}
}
}
setMethod.setAccessible(true);
setMethod.invoke(bean, beanValue);
}
}
}
Field[] fields = bean.getClass().getDeclaredFields();
for(Field field : fields) {
if(field != null && field.isAnnotationPresent(DysAnnotation.class)) {
DysAnnotation dysA = field.getAnnotation(DysAnnotation.class);
Object beanValue = null;
if(dysA.name() != null && !"".equals(dysA.name())) {
String name = dysA.name();
beanValue = singletons.get(name); } else {
String nam = field.getName();
beanValue = singletons.get(nam);
for(String key : singletons.keySet()) {
if(field.getType().isAssignableFrom(singletons.get(key).getClass())) {
beanValue = singletons.get(key);
break;
}
}
}
field.setAccessible(true);
field.set(bean, beanValue);
}
}
}
} catch(Exception e) {
e.printStackTrace();
}
}
private void injectObject(){
try {
for(BeanDefinition beanDefinition:beanDefinitions) {
Object object = singletons.get(beanDefinition.getId());
if(object != null) {
PropertyDescriptor[] pds = Introspector.getBeanInfo(object.getClass()).getPropertyDescriptors();
for(PropertyDefinition propertyDefinition : beanDefinition.getProperties()) {
for(PropertyDescriptor pd : pds) {
if(propertyDefinition.getName().equals(pd.getName())) {
Method setMethod = pd.getWriteMethod();
if(setMethod != null) {
Object propertyV = singletons.get(propertyDefinition.getRef());
setMethod.setAccessible(true);
setMethod.invoke(object, propertyV);
}
break;
}
}
}
}
}
} catch(Exception e) {
e.printStackTrace();
}
}
private void initilizeBeans() {
for(BeanDefinition beanDefinition : beanDefinitions) {
try {
if(beanDefinition.getName() != null && !"".equals(beanDefinition.getName().trim())) {
singletons.put(beanDefinition.getId(), Class.forName(beanDefinition.getName()).newInstance());
}
}catch(Exception e) {
e.printStackTrace();
}
}
}
private void readXML(String filename) {
SAXReader saxReader = new SAXReader();
Document document = null;
try {
URL xmlpath = this.getClass().getClassLoader().getResource(filename);
document = saxReader.read(xmlpath);
Map<String, String> nsMap = new HashMap<String, String>();
nsMap.put("ns", "http://www.springframework.org/schema/beans");
XPath xsub = document.createXPath("//ns:beans/ns:bean");
xsub.setNamespaceURIs(nsMap);
List<Element> beans = xsub.selectNodes(document);
for(Element element:beans){
String id = element.attributeValue("id");
String clazz = element.attributeValue("class");
BeanDefinition beanDefinition = new BeanDefinition(id, clazz);
XPath xsubProperty = element.createXPath("ns:property");
xsubProperty.setNamespaceURIs(nsMap);
List<Element> propertyElement = xsubProperty.selectNodes(element);
for(Element ele:propertyElement) {
String name = ele.attributeValue("name");
String ref = ele.attributeValue("ref");
PropertyDefinition propertyDefinition = new PropertyDefinition(name, ref);
beanDefinition.getProperties().add(propertyDefinition);
}
beanDefinitions.add(beanDefinition);
}
}catch(Exception e) {
e.printStackTrace();
}
}
public Object getBean(String name) {
Object beanClass = singletons.get(name);
return beanClass;
}
}
反射 XMLUtil的更多相关文章
- 上次遗留下来的XMLUtil的问题
·在上周留下了一个关于XMLUtil的问题,问题大概是这样的,需要通过读取一个XML文件,然后在内存中生成一个对应的javaBean.之前写的那个很是糟糕,照着一个XML去写了一个"Util ...
- java反射机制的粗略理解
java反射机制: 涉及的对象:Class, Object, 函数:Class类:[forName(String className):static:getClass():public],Object ...
- 抽象工厂模式(JAVA反射)
实例代码(JAVA):模式动机 在工厂方法模式中具体工厂负责生产具体的产品,每一个具体工厂对应一种具体产品,工厂方法也具有唯一性,一般情况下,一个具体工厂中只有一个工厂方法或者一组重载的工厂方 ...
- 工厂方法模式(JAVA反射)
简单工厂模式的不足 在简单工厂模式中,只提供了一个工厂类,该工厂类处于对产品类进行实例化的中心位置,它知道每一个产品对象的创建细节,并决定何时实例化哪一个产品类.简单工厂模式最大的缺点是当有新 ...
- dom4j+反射实现bean与xml的相互转换
由于目前在工作中一直用的dom4j+反射实现bean与xml的相互转换,记录一下,如果有不正确的地方欢迎大家指正~~~ 一.反射机制 在此工具类中使用到了反射技术,所以提前也看了一些知识点,例如:ht ...
- 隐私泄露杀手锏 —— Flash 权限反射
[简版:http://weibo.com/p/1001603881940380956046] 前言 一直以为该风险早已被重视,但最近无意中发现,仍有不少网站存在该缺陷,其中不乏一些常用的邮箱.社交网站 ...
- Java学习之反射机制及应用场景
前言: 最近公司正在进行业务组件化进程,其中的路由实现用到了Java的反射机制,既然用到了就想着好好学习总结一下,其实无论是之前的EventBus 2.x版本还是Retrofit.早期的View注解框 ...
- 关于 CSS 反射倒影的研究思考
原文地址:https://css-tricks.com/state-css-reflections 译者:nzbin 友情提示:由于演示 demo 的兼容性,推荐火狐浏览.该文章篇幅较长,内容庞杂,有 ...
- 编写高质量代码:改善Java程序的151个建议(第7章:泛型和反射___建议106~109)
建议106:动态代理可以使代理模式更加灵活 Java的反射框架提供了动态代理(Dynamic Proxy)机制,允许在运行期对目标类生成代理,避免重复开发.我们知道一个静态代理是通过主题角色(Prox ...
随机推荐
- ASP.NET Core 2.0 in Docker on Windows Containers
安装Docker for Windows https://store.docker.com/editions/community/docker-ce-desktop-windows 要想将一个ASP. ...
- k8s 入门系列之介绍篇
•Kubernetes介绍1.背景介绍 云计算飞速发展 - IaaS - PaaS - SaaS Docker技术突飞猛进 - 一次构建,到处运行 - 容器的快速轻量 - 完整的生态环境2.什么是ku ...
- Python3.5 学习七
心灵鸡汤 好书推荐:消费者行为学.围城.活着.盲井.三体.北京折叠 静态方法: @staticmethod #名义上归类管,和类没什么关系,只是引用用"." 在静态方法中,无法访问 ...
- Python3.5 学习四
装饰器 定义:本质是函数,装饰其他函数,即为其他函数添加附加功能的 原则: 1 不能修改被装饰函数的源代码 2 不能改变被装饰函数的调用方式(对于被装饰函数来说完全透明,不会受影响) 实现装饰器功能的 ...
- leecode刷题(22)-- 反转数组
leecode刷题(22)-- 反转数组 反转数组 反转一个单链表. 示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3-> ...
- S2-052复现过程(附POC利用)
漏洞编号:CVE-2017-9805(S2-052) 影响版本:Struts 2.5 - Struts 2.5.12 漏洞概述:问题出现在struts2-rest-plugin插件XStreamHan ...
- [Flex] 组件Tree系列 —— 运用variableRowHeight和wordWrap设置可变行高
mxml: <?xml version="1.0" encoding="utf-8"?> <!--功能描述:运用variableRowHeig ...
- 高性能缓存服务器Varnish
一.Varnish概述 Varnish是一款高性能的.开源的反向代理服务器和缓存服务器,计算机系统的除了有内存外,还有CPU的L1.L2,甚至L3级别的缓存,Varnish的设计架构就是利用操作系统的 ...
- C语言奇淫技巧,字符串的三种表示方法,不会用不是合格的程序员
1.在C语言中,是将字符串作为字符数组来处理的,字符串是逐个存放到数组元素中的 例如用一个一维的字符数组存放字符串"I am a boy.",如下代码: char c[12] = ...
- Laravel5.5 使用第三方Vendor添加注册验证码
Laravel5系列的验证码添加通用,使用第三方验证码即可完美实现.这里记录下具体步骤吧,以备不时之需. 第一步:使用composer 安装 验证码库 composer require mews/ca ...