- 浏览: 4905136 次
- 性别:
- 来自: 南京
文章分类
- 全部博客 (2844)
- java (1094)
- hadoop (37)
- jvm (39)
- hbase (11)
- sql (25)
- 异常 (83)
- div css (6)
- 数据库 (95)
- 有趣的code (15)
- struts2 (6)
- spring (124)
- js (44)
- 算法 (65)
- linux (36)
- hibernate (7)
- 中间件 (78)
- 设计模式 (2)
- 架构 (275)
- 操作系统 (91)
- maven (35)
- tapestry (1)
- mybatis (9)
- MQ (101)
- zookeeper (18)
- 搜索引擎,爬虫 (208)
- 分布式计算 (45)
- c# (7)
- 抓包 (28)
- 开源框架 (45)
- 虚拟化 (12)
- mongodb (15)
- 计算机网络 (2)
- 缓存 (97)
- memcached (6)
- 分布式存储 (13)
- scala (5)
- 分词器 (24)
- spark (104)
- 工具 (23)
- netty (5)
- Mahout (6)
- neo4j (6)
- dubbo (36)
- canal (3)
- Hive (10)
- Vert.x (3)
- docker (115)
- 分布式追踪 (2)
- spring boot (5)
- 微服务 (56)
- 淘客 (5)
- mesos (67)
- php (3)
- etcd (2)
- jenkins (4)
- nginx (7)
- 区块链 (1)
- Kubernetes (92)
- 驾照 (1)
- 深度学习 (15)
- JGroups (1)
- 安全 (5)
- 测试 (16)
- 股票 (1)
- Android (2)
- 房产 (1)
- 运维 (6)
- 网关 (3)
最新评论
-
明兜3号:
部署落地+业务迁移 玩转k8s进阶与企业级实践技能(又名:Ku ...
Kubernetes系统常见运维技巧 -
q328965539:
牛掰啊 资料收集的很全面
HDFS小文件处理解决方案总结+facebook(HayStack) + 淘宝(TFS) -
guichou:
fluent挂载了/var/lib/kubelet/pods目 ...
kubernetes上部署Fluentd+Elasticsearch+kibana日志收集系统 -
xu982604405:
System.setProperty("java.r ...
jmx rmi 穿越防火墙问题及jmxmp的替代方案 -
大漠小帆:
麻烦问下,“获取每个Item相似性最高的前N个Item”,这个 ...
协同过滤推荐算法在MapReduce与Spark上实现对比
最近项目用到jaxb解析xml文件。需要定义一个javaBean模型。
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {"sender","sheet" })
@XmlRootElement(name = "AUTO ")
public class DataFile{
@XmlElement(name="Sender",required = true)
protected Sender sender;
public Sender getSender() {
return sender;
}
public void setSender(Sender sender) {
this.sender = sender;
}
现在遇到个问题,就是@XmlRootElement(name = "AUTO ")这个节点是动态的。但是注解在运行期间是改变不了得。那只有改字节码了。Asm?cglib? javassist?..等等修改字节码的库。比较方便的还是cglib和javassist吧。因为它们都是对Asm的封装。
/**
* 运行时动态修改注解值
* @author c_zhangzengmin002
*
*/
public class ClassUtils {
//缓存加载器加载的calss对象
private static final Map<String, Class<?>> classPoolMap=new ConcurrentHashMap<String, Class<?>>();
private ClassUtils(){}
/**
* 运行时动态修改注解值
* @param Class
*
* @param entityClassName
* 待映射的实体全限定类名
* @param annotation 需要修改的注解全限定类名
* @param annotationAttrName 注解属性名称
* @param annotationVal 注解属性值
*
* @return 映射后的类对象
*/
public static Class<?> updateAnnotationForClass(String entityClassName,String annotationClassName, String annotationAttrName,String annotationVal)throws Exception {
Class<?> c = null;
if(StringUtil.isBlank(entityClassName) || StringUtil.isBlank(annotationClassName)
|| StringUtil.isBlank(annotationAttrName) || StringUtil.isBlank(annotationVal)){
return c;
}
c=classPoolMap.get(annotationVal);
if(null!=c){
return c;
}
try {
ClassPool classPool = ClassPool.getDefault();
classPool.appendClassPath(new ClassClassPath(ClassUtils.class));
//classPool.importPackage("javax.persistence");
CtClass clazz = classPool.get(entityClassName);
clazz.defrost();
ClassFile classFile = clazz.getClassFile();
ConstPool constPool = classFile.getConstPool();
Annotation tableAnnotation = new Annotation(
annotationClassName, constPool);
tableAnnotation.addMemberValue(annotationAttrName, new StringMemberValue(
annotationVal, constPool));
// 获取运行时注解属性
AnnotationsAttribute attribute = (AnnotationsAttribute) classFile
.getAttribute(AnnotationsAttribute.visibleTag);
attribute.addAnnotation(tableAnnotation);
classFile.addAttribute(attribute);
classFile.setVersionToJava5();
// clazz.writeFile();
AnnotationClassLoader loader = new AnnotationClassLoader(
ClassUtils.class.getClassLoader());
//加载器加载class文件,一个加载器只会加载一次路径名称相同的calss
//这里因为只改了注解,类路径是没有改变的。
c = clazz.toClass(loader, null);
if(null!=c){
classPoolMap.put(annotationVal, c);
}
} catch (Exception e) {
e.printStackTrace();
}
return c;
}
public static void main(String[] args)throws Exception { Class<?> clazz3 = ClassUtils.updateAnnotationForClass(
"com.dep.jaxb.DataFile","javax.xml.bind.annotation.XmlRootElement","name", "AUTO_IDS");
System.out.println("修改后的@XmlRootElement: " + clazz3.getAnnotation(XmlRootElement.class).name());
}
}
/**
* <p>
* Description:根据class类型,将文件流转换成对象。在这个过程中会对文件进行编码处理
* </p>
* <p>
* Create Time:2012-9-31
* </p>
*
* @author zzm
* @param Class
* <?>
* @param BufferedReader
* 文件流
* @throws Exception
*/
public static Object unmarshalClaim(Class<?> c, BufferedReader in)
throws Exception {
StringBuilder buffer = null;
JAXBContext jaxbContext;
try {
buffer = new StringBuilder();
String line = "";
if (in != null) {
while ((line = in.readLine()) != null) {
buffer.append(line);
}
}
//所取头结点名称
String dataFileName=StringUtil.getXMlHandleNodeString(buffer);
//获取动态class对象
Class<?> calss=ClassUtils.updateAnnotationForClass(Constant.DATAFILE_CLASS_NAME, Constant.XMLROOTELEMENT_CLASS_NAME,
Constant.NAME_STR, dataFileName);
if(null!=calss){
c=calss;
}
String xmlString = StringUtil.getXMlString(buffer);
StreamSource streamSource = new StreamSource(new StringReader(
xmlString));
// 加载映射bean类
jaxbContext = JAXBContext.newInstance(c);
// 创建解析
Unmarshaller um = jaxbContext.createUnmarshaller();
return unmarshalClaim(um,streamSource);
} catch (Exception e) {
log.error(e);
e.printStackTrace();
throw new BasicException(e.getMessage());
}finally{
in.close();
}
}
问题来了!
public static void unmarshalClaim()throws Exception {
String s = "E:\\Documents and Settings\\c_zhangzengmin002\\桌面\\xml\\1.xml";
//将xml动态的转换成相应的javaBean对象
Object obj = JAXBUtil.unmarshalClaim(DataFile.class, s);
//这样问题来了,这里强转报错了。因为要动态获取的class对象。这里是使用不同的加载器加载的calss对象实例。不同加载器加载的对象实例是不一样的。
//DataFile newStudent = (DataFile) obj
}
不同加载器加载同class对象实例是不一样的。当一个类实例化时,首先会对其父类实例化。
解决:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {"sender","sheet" })
@XmlRootElement(name = "AUTO ")
public class DataFile extends AbstractDataFile{
@XmlElement(name="Sender",required = true)
protected Sender sender;
public Sender getSender() {
return sender;
}
public void setSender(Sender sender) {
this.sender = sender;
}
public abstract class AbstractDataFile {
public Sender getSender() {return null;};
}
public static void unmarshalClaim()throws Exception {
String s = "E:\\Documents and Settings\\c_zhangzengmin002\\桌面\\xml\\1.xml";
//自定义加载器加载的对象
Object newStudent2 = JAXBUtil.unmarshalClaim(DataFile.class, s3);
//默认加载器加载的对象
DataFile dataFile =new DataFile();
AbstractDataFile newStudent = (AbstractDataFile) newStudent2;
System.out.println(dataFile.getClass().getClassLoader());
System.out.println(newStudent.getClass().getClassLoader());
//父类的加载器 System.out.println(dataFile.getClass().getSuperclass().getClassLoader());
//父类的加载器 System.out.println(newStudent.getClass().getSuperclass().getClassLoader());
}
运行结果:
sun.misc.Launcher$AppClassLoader@82ba41
com.dep.util.EntityClassLoader@a39137
sun.misc.Launcher$AppClassLoader@82ba41
sun.misc.Launcher$AppClassLoader@82ba41
结果发现,父类都是由同一加载器加载的。目前解决此问题就想到这个办。应该还有其他的方法。
发表评论
-
Kryo 使用指南
2017-12-05 20:14 18711、Kryo 的简介 Kryo 是一个快速序列化/ ... -
spring session序列化问题排查
2017-12-01 19:07 6126严重: Servlet.service() for ser ... -
利用junit对springMVC的Controller进行测试
2017-11-30 16:26 1387平时对junit测试service/D ... -
Java内存模型之重排序
2017-11-29 09:44 806在执行程序时,为了提供性能,处理器和编译器常常会对指令进行重 ... -
pmd spotbugs 文档
2017-11-28 10:02 0https://pmd.github.io/pmd/pmd ... -
PMD、FindBug、checkstyle、sonar这些代码检查工具的区别?各自的侧重点是什么?
2017-11-28 10:01 2087可以说都是代码静态分析工具,但侧重点不同。pmd:基于源代码 ... -
阿里巴巴Java代码规约插件p3c-pmd使用指南与实现解析
2017-11-23 17:09 1522阿里巴巴Java代码规约插件安装 阿里Java代码规 ... -
静态分析工具PMD使用说明 (文章来源: Java Eye)
2017-11-23 17:07 1090质量是衡量一个软件是否成功的关键要素。而对于商业软件系统,尤 ... -
MyBatis 使用 MyCat 实现多租户的一种简单思路
2017-11-20 18:27 2788本文的多租户是基于多数据库进行实现的,数据是通过不同数据库进 ... -
Spring+MyBatis实现数据库读写分离方案
2017-11-20 17:15 1010百度关键词:spring mybatis 多数据源 读写分离 ... -
数据库连接池druid wallfilter配置
2017-11-20 11:38 1190使用缺省配置的WallFilter <be ... -
java restful 实体封装
2017-11-16 09:47 1528package com.mogoroom.bs.commo ... -
dak
2017-11-15 11:21 0package zzm; import jodd.ht ... -
Java内存模型之从JMM角度分析DCL
2017-11-15 09:35 578DCL,即Double Check Lock,中卫双重检查锁 ... -
Java 打印堆栈的几种方法
2017-11-14 09:36 4635java 中可以通过 eclipse 等工具直接打印堆栈, ... -
Servlet Session学习
2017-11-10 09:25 492HTTP 是一种"无状 ... -
浅析Cookie中的Path与domain
2017-11-10 09:26 1001Path – 路径。指定与co ... -
入分析volatile的实现原理
2017-11-08 09:47 610通过前面一章我们了解了synchronized是一个重量级的 ... -
Spring MVC-ContextLoaderListener和DispatcherServlet
2017-11-15 09:35 622Tomcat或Jetty作为Servlet ... -
搭建spring框架的时候,web.xml中的spring相关配置,可以不用配置ContextLoaderListener(即只配DispatcherServl
2017-11-07 18:27 1371搭建spring框架的时候,web.xml中的sprin ...
相关推荐
JAXB-XML注解-应用
jaxb-api jaxb-impl jaxb-xjc jaxws-rt 这四个文件对应的jar包
赠送jar包:jaxb-core-2.2.10-b140310.1920.jar; 赠送原API文档:jaxb-core-2.2.10-b140310.1920-javadoc.jar; 赠送源代码:jaxb-core-2.2.10-b140310.1920-sources.jar; 赠送Maven依赖信息文件:jaxb-core-...
赠送jar包:jackson-module-jaxb-annotations-2.2.3.jar; 赠送原API文档:jackson-module-jaxb-annotations-2.2.3-javadoc.jar; 赠送源代码:jackson-module-jaxb-annotations-2.2.3-sources.jar; 赠送Maven依赖...
赠送jar包:jaxb-svg11-1.0.2.jar; 赠送原API文档:jaxb-svg11-1.0.2-javadoc.jar; 赠送源代码:jaxb-svg11-1.0.2-sources.jar; 赠送Maven依赖信息文件:jaxb-svg11-1.0.2.pom; 包含翻译后的API文档:jaxb-svg11...
赠送jar包:jaxb-svg11-1.0.2.jar; 赠送原API文档:jaxb-svg11-1.0.2-javadoc.jar; 赠送源代码:jaxb-svg11-1.0.2-sources.jar; 赠送Maven依赖信息文件:jaxb-svg11-1.0.2.pom; 包含翻译后的API文档:jaxb-svg11...
赠送jar包:jackson-module-jaxb-annotations-2.7.8.jar; 赠送原API文档:jackson-module-jaxb-annotations-2.7.8-javadoc.jar; 赠送源代码:jackson-module-jaxb-annotations-2.7.8-sources.jar; 赠送Maven依赖...
JAXB能够使用Jackson对JAXB注解的支持实现(jackson-module-jaxb-annotations),既方便生成XML,也方便生成JSON,这样一来可以更好的标志可以转换为JSON对象的JAVA类。JAXB允许JAVA人员将JAVA类映射为XML表示方式,...
jaxb-api jaxb-impl jar
赠送jar包:jaxb-api-2.3.1.jar; 赠送原API文档:jaxb-api-2.3.1-javadoc.jar; 赠送源代码:jaxb-api-2.3.1-sources.jar; 包含翻译后的API文档:jaxb-api-2.3.1-javadoc-API文档-中文(简体)-英语-对照版.zip ...
赠送jar包:jaxb-runtime-2.3.5.jar; 赠送原API文档:jaxb-runtime-2.3.5-javadoc.jar; 赠送源代码:jaxb-runtime-2.3.5-sources.jar; 赠送Maven依赖信息文件:jaxb-runtime-2.3.5.pom; 包含翻译后的API文档:...
赠送jar包:jaxb-runtime-2.3.5.jar; 赠送原API文档:jaxb-runtime-2.3.5-javadoc.jar; 赠送源代码:jaxb-runtime-2.3.5-sources.jar; 赠送Maven依赖信息文件:jaxb-runtime-2.3.5.pom; 包含翻译后的API文档:...
赠送jar包:jaxb-impl-2.2.10-b140310.1920.jar; 赠送原API文档:jaxb-impl-2.2.10-b140310.1920-javadoc.jar; 赠送源代码:jaxb-impl-2.2.10-b140310.1920-sources.jar; 赠送Maven依赖信息文件:jaxb-impl-...
赠送jar包:jaxb-api-2.3.0.jar; 赠送原API文档:jaxb-api-2.3.0-javadoc.jar; 赠送源代码:jaxb-api-2.3.0-sources.jar; 赠送Maven依赖信息文件:jaxb-api-2.3.0.pom; 包含翻译后的API文档:jaxb-api-2.3.0-...
有关Maven项目中缺少jaxb-api的异常报错解决,jaxb-api-2.3.0.jar
赠送jar包:jaxb-api-2.3.0.jar; 赠送原API文档:jaxb-api-2.3.0-javadoc.jar; 赠送源代码:jaxb-api-2.3.0-sources.jar; 赠送Maven依赖信息文件:jaxb-api-2.3.0.pom; 包含翻译后的API文档:jaxb-api-2.3.0-...
赠送jar包:jackson-module-jaxb-annotations-2.7.8.jar; 赠送原API文档:jackson-module-jaxb-annotations-2.7.8-javadoc.jar; 赠送源代码:jackson-module-jaxb-annotations-2.7.8-sources.jar; 赠送Maven依赖...
jaxb-api-2.0.jar jaxb-api-2.0.jar
赠送jar包:jaxb-api-2.2.2.jar; 赠送原API文档:jaxb-api-2.2.2-javadoc.jar; 赠送源代码:jaxb-api-2.2.2-sources.jar; 赠送Maven依赖信息文件:jaxb-api-2.2.2.pom; 包含翻译后的API文档:jaxb-api-2.2.2-...
赠送jar包:jaxb-api-2.2.11.jar; 赠送原API文档:jaxb-api-2.2.11-javadoc.jar; 赠送源代码:jaxb-api-2.2.11-sources.jar; 赠送Maven依赖信息文件:jaxb-api-2.2.11.pom; 包含翻译后的API文档:jaxb-api-...