从 注解和继承 到 JAXB中的注意事项
从 注解和继承 到 JAXB中的注意事项
注解在继承中的行为
如果一个父类添加了一个注解,子类是否能取到这个注解呢?如下
package inheritance;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
public class Main {
public static void main(String[] args) {
Type t = Son.class.getAnnotation(Type.class);
if (t != null) {
System.out.println(t.name());
}
}
}
@Type(name = "Father")
class Father {
}
class Son extends Father {
}
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface Type {
String name();
}
如上代码,注解不会被子类继承。如果想注解也被子类继承,该怎么办呢?
只需要在注解定义里修改一下,添加@Inherited
@Inherited
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface Type {
String name();
}
关于@Inherited需要注意的:
- 只能作用于继承,不能作用于接口实现
JAXB 的注解
@XmlAccessorType用于标注该类的成员是否用于绑定到XML,例如XmlAccessType.PUBLIC_MEMBER表示,所有public字段都会被绑定(除去@XmlElement和@XmlTransient的标记,他们优先级更高)。
这个注解就是标记了@Inherited。
我们知道序列化顺序可以由@XmlType(propOrder)去设置,那么继承后是什么样子呢?
package inheritance;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
public class Main {
public static void main(String[] args) throws Exception {
JAXBContext ctx = JAXBContext.newInstance(Son.class);
Marshaller ms = ctx.createMarshaller();
ms.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
ms.marshal(new Son(), System.out);
}
}
@XmlRootElement(name = "Father")
@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
@XmlType(propOrder = { "two", "one" })
class Father {
@XmlElement
private String one = "one";
@XmlElement
public String two = "two";
}
@XmlRootElement(name = "Son")
@XmlAccessorType(XmlAccessType.NONE)
@XmlType(propOrder = { "four", "three" })
class Son extends Father {
@XmlElement
public String three = "three";
@XmlElement
public String four = "four";
}
上述代码会输出
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Son>
<two>two</two>
<one>one</one>
<four>four</four>
<three>three</three>
</Son>
即父类成员先序列化,再子类成员,顺序由各自类的@XmlType设置的顺序决定。
那么如果子类一定想控制父类成员的序列化顺序,而且不同的子类还想各自定义父类成员的序列化顺序怎么办?(CNM, 屁事真多)
好吧,JAXB还是可以满足这种屁事儿的。
package inheritance;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlType;
public class Main {
public static void main(String[] args) throws Exception {
go(Son.class);
go(Daughter.class);
}
private static void go(Class<?> clz) throws Exception {
JAXBContext ctx = JAXBContext.newInstance(clz);
Marshaller ms = ctx.createMarshaller();
ms.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
ms.marshal(clz.newInstance(), System.out);
}
}
@XmlTransient
class Father {
@SuppressWarnings("unused")
private String one = "one";
public String two = "two";
}
@XmlRootElement(name = "Son")
@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
@XmlType(propOrder = { "four", "three", "two" })
class Son extends Father {
@XmlElement
public String three = "three";
@XmlElement
public String four = "four";
}
@XmlRootElement(name = "Daughter")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder = { "one", "two", "three", "four" })
class Daughter extends Father {
@XmlElement
public String three = "three";
@XmlElement
public String four = "four";
}
上述代码将输出
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Son>
<four>four</four>
<three>three</three>
<two>two</two>
</Son>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Daughter>
<one>one</one>
<two>two</two>
<three>three</three>
<four>four</four>
</Daughter>
解释如下:
- 要想每个子类去控制父类成员,必须把父类标记成@XmlTransient,而且子类@XmlType的propOrder要负责所有自己要序列化的成员。
- 类Son的@XmlAccessorType标注为XmlAccessType.PUBLIC_MEMBER,意味着Son只序列化Public字段。加上父类的public成员,Son共有3个public成员,所以@XmlType(propOrder)写了三个成员的顺序。
- 类Daughter的@XmlAccessorType标注为XmlAccessType.FIELD,意味着所有父类和自己的成员都会序列化,于是标记四个成员的顺序。
忽略dtd
import java.io.FileReader;
import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.sax.SAXSource;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
public class Demo2 {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(MyBean.class);
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
spf.setFeature("http://xml.org/sax/features/validation", false);
spf.setNamespaceAware(true);
XMLReader xmlReader = spf.newSAXParser().getXMLReader();
InputSource inputSource = new InputSource(
new FileReader("myfile.xml"));
SAXSource source = new SAXSource(xmlReader, inputSource);
Unmarshaller unmarshaller = jc.createUnmarshaller();
MyBean foo = (MyBean) unmarshaller.unmarshal(source);
}
}
从 注解和继承 到 JAXB中的注意事项的更多相关文章
- JAXB中的@XmlRootElement注解详解
@Retention(value=RUNTIME) @Target(value=TYPE) public @interface XmlRootElement @Inherited @Retention ...
- Spring 注解驱动(二)Servlet 3.0 注解驱动在 Spring MVC 中的应用
Spring 注解驱动(二)Servlet 3.0 注解驱动在 Spring MVC 中的应用 Spring 系列目录(https://www.cnblogs.com/binarylei/p/1019 ...
- 【Java EE 学习 24 下】【注解在数据库开发中的使用】【反射+注解+动态代理在事务中的应用service层】
一.使用注解可以解决JavaBean和数据库中表名不一致.字段名不一致.字段数量不一致的问题. 1.Sun公司给jdbc提供的注解 @Table.@Column.@Id.@OneToMany.@One ...
- 继承在WCF中的问题和解决办法
1. 问题 假设有代码如下: [ServiceContract] public interface IA { [OperationContract] string M1(); } [ServiceCo ...
- android开发中关于继承activity类中方法的调用
android开发中关于继承activity类中的函数,不能在其他类中调用其方法. MainActivity.java package com.example.testmain; import and ...
- C#继承机制 C#中的继承符合下列规则
1.继承是可传递的.如果C从B中派生,B又从A中派生,那么C不仅继承了B中声明的成员,同样也继承了A中的成员.Object 类作为所有类的基类. 2.派生类应当是对基类的扩展.派生类可以添加新的成员, ...
- python基础语法15 面向对象2 继承,多态,继承json模块中JSONEncoder,并派生出新的功能
继承 1.什么是继承? 继承是一种新建类的方式,新建的类称之为子类或派生类,继承的父类称之为基类或超类. - 在Python中,一个子类可以继承多个父类.(面试可能会问) - 在其它语言中,一个子类只 ...
- C++ //多继承语法 C++中允许一个类继承多个类
1 //多继承语法 C++中允许一个类继承多个类 2 #include <iostream> 3 #include <string> 4 using namespace std ...
- 关于JDBC学习过程中的注意事项(分享自己犯过的错误,写给初学JDBC的小伙伴的八条建议)
关于JDBC学习过程中的注意事项(分享自己犯过的错误,写给初学JDBC的小伙伴的八条建议) 前言:最近在学习JDBC,总结了几个小问题,特地分享给大家,让大家不要犯这样的错误,也希望大家养成学会总结的 ...
随机推荐
- Python项目依赖并生成requirements.txt
一起开发项目的时候总是要搭建环境和部署环境的,这个时候必须得有个python第三方包的list,一般都叫做requirements.txt. 如果一个项目使用时virtualenv环境,还好办 pip ...
- 设计模式—模板方法(template method)
一.定义 百度百科给的定义:定义一个操作中的算法骨架(稳定),而将一些步骤延迟到子类中(变化).Template Method使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤. 如何做到 ...
- PIL库的总结及运用
PIL库的总结:(以代码形式) #date: 2018/11/15 from PIL import Image,ImageFilter,ImageDraw,ImageFont #####除了缩略图的方 ...
- 继续写java和socket
上回书说道,java中通过socket链接的两台计算机可以通过两端的两个Socket对象来读取和发送来自流的信息所以对于客户端和服务端只需要封装一个相同的收发信息的窗口就好 代码如下 package ...
- day-14带参装饰器、迭代器
带参装饰器 通常,装饰器为被装饰的函数添加新功能,需要外界的参数 -- outer参数固定一个,就是func -- inner参数固定同被装饰的函数,也不能添加新参数 -- 可以借助函数的嵌套定义, ...
- python-简单的登陆接口
# 工作需求2 编写登陆接口(1输入用户名密码 2认证成功后显示欢迎信息 3输错三次后锁定)# 实现思路 1.用户名和密码是否正确,全部正确可正常登陆 输错三次无法登陆 2.是否已经注册 未注册 无法 ...
- js中this最简单清晰的解释
引自 https://www.cnblogs.com/huangwentian/p/6854472.html#commentform ① this指向的,永远只可能是对象! ② this ...
- MQTT研究之EMQ:【JAVA代码构建X509证书【续集】】
openssl创建私钥,获取公钥,创建证书都是比较简单的,就几个指令,很快就可以搞定,之所以说简单,是因为证书里面的基本参数配置不需要我们组装,只需要将命令行里面需要的几个参数配置进去即可.但是呢,用 ...
- kvm中重命名虚拟机
kvm中重命名虚拟机 1.查看虚拟机 [root@linux ~]# virsh list --all Id Name State ---------------------------------- ...
- 创建maven工程的时候卡死的解决办法
在idea的maven,runner,properties里面添加 archetypeCatalog=internal