您的位置:首页 > 编程语言 > Java开发

java注解(一)概念及JABX使用例子

2017-10-17 18:12 429 查看
一、概念____________________________________________________________

 

注解,作用类似有xml配置文件,避免同一份信息保存在两个地方。

@Override,@Deprecated (key,value)都省略了

@Retention(RetentionPolicy.RUNTIME)只有一个键时,key可以省略

@SupressWarnings({"uncheck", "unused"})大括号表示数组类型,key只有一个,省略

@XmlElement(name = "id")配置参数,键值对

 

 

注解用@interface用来声明定义,元注解,在java.lang.annotation包里面,有4个

 

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Documented {//are to be documented by javadoc and similar tools by default
}

 

 

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Inherited {//
}

 

 

//@Retention保留策略: CLASS、RUNTIME和SOURCE,分别表示注解保存在类文件、JVM运行时刻和源代码中。
//只有RUNTIME,才能在运行时刻通过反射API来获取到注解的信息,类似SSH的xml配置文件的注入作用
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Retention {
RetentionPolicy value();
}

 

 

//@Target用来声明注解的作用范围,如类型、方法和域等
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
ElementType[] value();
}

 

二、JAXB例子____________________________________________________________

 

JAXB(Java Architecture for XML Binding) 

 

/**
* @author timeriver.wang
* @date 2013-01-09 8:07:01 PM
*/
@XmlRootElement
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Student {
private String id;
private String name;
public String getId() {
return id;
}
@XmlElement(name = "id")
public void setId(String id) {
this.id = id;
}
@XmlElement(name = "name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

 

 

/**
* @author timeriver.wang
* @date 2013-01-09 8:07:09 PM
*/
@XmlRootElement
public class Teacher {
private String id;
private String name;
private List<Student>students;
@XmlAttribute (name = "tid")
public String getId() {
return id;
}
public void setId( String id ) {
this.id = id;
}
@XmlElement(name = "tname")
public String getName() {
return name;
}
public void setName( String name ) {
this.name = name;
}
@XmlElement(name = "students")
public List<Student> getStudents() {
return students;
}
public void setStudents( List<Student> students ) {
this.students = students;
}
}

 

 

/**
* @author timeriver.wang
* @date 2013-01-09 8:08:15 PM
*/
public class Test {
public static void main( String[] args )throws Exception {
//        toXml();
//        toStu();
toXmls();
}

public static void toXml()throws Exception {
Student stu = new Student();
stu.setId( "007" );
stu.setName( "zhouxingxing" );

File file = new File( "D:/stu.xml" );
JAXBContext jaxbContext = JAXBContext.newInstance( Student.class );

Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

jaxbMarshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, true );

jaxbMarshaller.marshal( stu, file );
jaxbMarshaller.marshal( stu, System.out );
}

public static void toStu()throws Exception {
File file = new File( "D:/stu.xml" );
JAXBContext jaxbContext = JAXBContext.newInstance( Student.class );
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Student stu = (Student) jaxbUnmarshaller.unmarshal( file );
System.out.println( stu );
}

public static void toXmls()throws Exception {
Student stu = new Student();
stu.setId( "007" );
stu.setName( "zhouxingxing" );
Student stu2 = new Student();
stu2.setId( "008" );
stu2.setName( "dawenxi" );
List<Student>students = new ArrayList<Student>();
students.add( stu );
students.add( stu2 );
Teacher teacher = new Teacher();
teacher.setId( "101" );
teacher.setName( "DASHI" );
teacher.setStudents( students );
File file = new File( "D:/teacher.xml" );
JAXBContext jaxbContext = JAXBContext.newInstance( Teacher.class );

Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

jaxbMarshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, true );

jaxbMarshaller.marshal( teacher, file );
jaxbMarshaller.marshal( teacher, System.out );
}
}

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: