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. Python 爬虫入门实例(爬取小米应用商店的top应用apk)

    一,爬虫是什么? 爬虫就是获取网络上各种资源,数据的一种工具.具体的可以自行百度. 二,如何写简单爬虫 1,获取网页内容 可以通过 Python(3.x) 自带的 urllib,来实现网页内容的下载. ...

  2. csv文件乱码

    问题描述: 生成的csv文件,设置为UTF-8格式,在windows上用EXCEL打开的话会乱码,在linux上用vim或者cat打开查看正常:设置为GBK格式的话,在windows上用EXCEL打开 ...

  3. SQL SERVER 查找锁信息

    通过系统的存储过程  sp_who 或 sp_who2 可以查找出所有的锁信息, 但是看不出是哪个表, 什么语句 当使用 sp_who 或 sp_who2 查找锁信息的时候, 有个 spid 信息,  ...

  4. Apache commons StringUtils 在运行时出现NoClassDefError错误的解决方法

    Apache commons StringUtils 在运行时出现NoClassDefError错误的解决方法 在用tomcat运行WEB项目,并且使用了StringUtils包的时候,会出现 jav ...

  5. PostMan工具使用之基础篇

    PostMan工具使用之基础篇 一.什么是PostMan Postman一款非常流行的API调试工具.(其他测试工具 Jmeter.soapUI) 二.下载及安装: 1.下载: 下载地址:https: ...

  6. WPF 内部Template 动画板 无法冻结此 Storyboard 时间线树供跨线程使用

    解决此问题,需要一定的想象力. 换个思路即可 大体是 使用Tag或者别无用的可以输入数值的属性,或者附加属性也可以的.来绑定到你要动画的属性 当然这个过程中要使用转换器了 我给出一个关于Button ...

  7. kali linux之无线渗透(续)

    Airolib 设计用于存储ESSID和密码列表,计算生成不变的PMK(计算资源消耗型) PMK在破解阶段被用于计算PTK(速度快,计算资源要求少) 通过完整性摘要值破解密码SQLite3数据库存储数 ...

  8. eclipse的debug模式经常自动跳到TheadPoolExecutor的断点

    1.问题:eclipse的debug模式经常自动跳到TheadPoolExecutor的断点,实在烦人 解决方法是: 在eclipse中选择Window->Preference->Java ...

  9. [flex] as3.0 实现基于air的简单浏览器

    <?xml version="1.0" encoding="utf-8"?> <s:WindowedApplication xmlns:fx= ...

  10. webpack 中文文档

    webpack 最强最详细中文文档 https://doc.webpack-china.org/guides/getting-started/#- webpack多页应用架构系列 http://web ...