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的更多相关文章

  1. 上次遗留下来的XMLUtil的问题

    ·在上周留下了一个关于XMLUtil的问题,问题大概是这样的,需要通过读取一个XML文件,然后在内存中生成一个对应的javaBean.之前写的那个很是糟糕,照着一个XML去写了一个"Util ...

  2. java反射机制的粗略理解

    java反射机制: 涉及的对象:Class, Object, 函数:Class类:[forName(String className):static:getClass():public],Object ...

  3. 抽象工厂模式(JAVA反射)

    实例代码(JAVA):模式动机     在工厂方法模式中具体工厂负责生产具体的产品,每一个具体工厂对应一种具体产品,工厂方法也具有唯一性,一般情况下,一个具体工厂中只有一个工厂方法或者一组重载的工厂方 ...

  4. 工厂方法模式(JAVA反射)

    简单工厂模式的不足     在简单工厂模式中,只提供了一个工厂类,该工厂类处于对产品类进行实例化的中心位置,它知道每一个产品对象的创建细节,并决定何时实例化哪一个产品类.简单工厂模式最大的缺点是当有新 ...

  5. dom4j+反射实现bean与xml的相互转换

    由于目前在工作中一直用的dom4j+反射实现bean与xml的相互转换,记录一下,如果有不正确的地方欢迎大家指正~~~ 一.反射机制 在此工具类中使用到了反射技术,所以提前也看了一些知识点,例如:ht ...

  6. 隐私泄露杀手锏 —— Flash 权限反射

    [简版:http://weibo.com/p/1001603881940380956046] 前言 一直以为该风险早已被重视,但最近无意中发现,仍有不少网站存在该缺陷,其中不乏一些常用的邮箱.社交网站 ...

  7. Java学习之反射机制及应用场景

    前言: 最近公司正在进行业务组件化进程,其中的路由实现用到了Java的反射机制,既然用到了就想着好好学习总结一下,其实无论是之前的EventBus 2.x版本还是Retrofit.早期的View注解框 ...

  8. 关于 CSS 反射倒影的研究思考

    原文地址:https://css-tricks.com/state-css-reflections 译者:nzbin 友情提示:由于演示 demo 的兼容性,推荐火狐浏览.该文章篇幅较长,内容庞杂,有 ...

  9. 编写高质量代码:改善Java程序的151个建议(第7章:泛型和反射___建议106~109)

    建议106:动态代理可以使代理模式更加灵活 Java的反射框架提供了动态代理(Dynamic Proxy)机制,允许在运行期对目标类生成代理,避免重复开发.我们知道一个静态代理是通过主题角色(Prox ...

随机推荐

  1. webservice需要的包以及demo

    包地址:http://pan.baidu.com/s/1qWyPgqo demo:http://pan.baidu.com/s/1dDvNJg9

  2. Bootstrap变形记

    bootstrap审美疲劳了,想个招换换样子,THINKING... 变形 >>> 哈,不用改已有代码,添加我的Harley.js即可,有空在玩... 真实好久不玩博客园了,200字 ...

  3. js自执行函数、调用递归函数、圆括号运算符、函数声明的提升

    前言 起因是我要在jquery的ajax中需要根据返回值来决定是否继续发起ajax请求,这是一个有条件的循环,符合条件就跳出.可以使用while循环的,但是想了想还是递归调用好用. 调用递归函数 递归 ...

  4. .Net Core使用HttpClient请求Web API注意事项

    HttpClient 使用HttpClient可以很方便的请求Web API,但在使用时有一些需要注意的地方,不然会给你的程序带来毁灭性的问题. HttpClient是一个继承了IDisposable ...

  5. 虚拟化 - Hyper-V

    不能和VMware.VirtualBox同时使用 网络 交换机其实就是指网卡,只不过是虚拟的 内部交换机 外部交换机

  6. iOS 安装包瘦身 (上篇)

    本文来自网易云社区 作者:饶梦云 1. 安装包组成 谈到 App 瘦身,最直接的想法莫过于分析一个安装包内部结构,了解其每一部分的来源.解压一个 ipa 包,拿到其 payload 中 app 文件的 ...

  7. HBase原理–所有Region切分的细节都在这里了

    本文由  网易云发布.   作者:范欣欣(本篇文章仅限内部分享,如需转载,请联系网易获取授权.)   Region自动切分是HBase能够拥有良好扩张性的最重要因素之一,也必然是所有分布式系统追求无限 ...

  8. Android Studio显示可视化编辑界面

    选中项目,依次展开“src/main/res/layout",双击"activity_main.xml",这样右侧就有“preview”选项卡了,点击activity_m ...

  9. 微信小程序获取当前经纬度并逆解析地址代码

    功能如标题. map.wxml代码如下: <!--miniprogram/pages/map/map.wxml--> <view><text>经度{{jd}}< ...

  10. lamp-linux-1

    LAMP编程之Linux(1) LAMP:Linux Apache MySQL PHP LNMP:Linux Nginx MySQL PHP WAMP:Windows Apache MySQL PHP ...