Java Annotation learning, javaannotation
Java annotations are widely used. The testing frameworks JUnit4, Spring, Hibernate, Struts, MyBatis, and JPA all support annotations. The Attribute feature in C # Corresponds to the annotation in Java. Annotations are used to make programming easier. But we need to know what Annotations do. It is best to be able to customize annotations in our own code to improve productivity.
Next, let's talk about Annotation, let's talk about basic knowledge, and then use an example to concatenate these strings.
------------------------------------------------------- Yuan annotation ----------------------------------------------------------------------------
Java annotation classification: Meta annotation, General annotation. Metadata is the data and information of data. In this case, the meta annotation is the annotation and annotation information. The meta annotations in java include: @ Retention @ Target @ Document @ Inherited:
@ DocumentIs used for javadoc. It is rarely used.
@ Retention: Define the annotation retention policy:
SOURCE: Only reserved in the SOURCE code. The compiled class file does not exist.
CLASS: It will be retained to the Class file, and the CLASS will be removed after the Class loader loads the CLASS into JVM.
RUNTIME: it is retained to the RUNTIME, that is, it can be used in JVM. This is the most common practice.
@ Target: Define the purpose of the annotation:
@ Target (ElementType. TYPE) // interface, class, enumeration, Annotation
@ Target (ElementType. FIELD) // The constant @ Target (ElementType. METHOD) // METHOD @ Target (ElementType. PARAMETER) // method PARAMETER @ Target (ElementType. CONSTRUCTOR) // CONSTRUCTOR @ Target (ElementType. LOCAL_VARIABLE) // local variable @ Target (ElementType. ANNOTATION_TYPE) // annotation @ Target (ElementType. PACKAGE) // PACKAGE
@ InheritedInherit other annotations
These are all meta annotations.
-------------------------------------------------------------- Annotation ---------------------------------------------------------------------------
Annotation itself is not an Annotation, but the Annotation we write will hide and inherit this class.
Annotation declaration:
1 @ Retention @ Target2 3 public @ interface annotation name {4 5}View Code
@ Interface, used to indicate annotations. The annotations written by interfaces are written by interfaces, that is, by writing methods in interfaces.
The method in the annotation is called the annotation element. Its syntax is:
|
Public elementType elementName () [default value]; |
ElementName: element name. The naming method is the method in java.
ElementType: the element type. The element types can be:
Native type: byte, short, int, long, float, double
String: String,
Date: Date
Class
Enum
You can set the default value.
Not to mention how to use it.
Annotation processing is implemented through reflection in Java.
Digress:Without reflection, there will be no frameworks in Java. It can be seen how important reflection is.
----------------------------------------------------- Example --------------------------------------------------------------------------------
Design an Input annotation to output attributes in Java bean into the Input element in HTML. This is what I wrote. It has no practical significance, but it is enough to describe the annotation. It includes the usage of the annotation:
Input annotation:
1 @ Target (value = {ElementType. FIELD}) 2 @ Retention (RetentionPolicy. RUNTIME) 3 public @ interface Input {4 public String value () default ""; // The element is of the common type 5 public String style () default ""; 6 public String clazz () default ""; 7 public String id () default ""; 8 public String title () default ""; 9 public String volidator () default ""; 10 public Checked checked () default @ Checked (checked = false); // The element is the annotation 11 public InputType type () default InputType. text; // The element is enumeration 12 13 14 enum InputType {15 text, radio, checkbox, button, password, file; 16} 17}Input annotation 1 @ Target (value = {ElementType. FIELD}) 2 @ Retention (RetentionPolicy. RUNTIME) 3 public @ interface Checked {4 public boolean checked () default false; 5}View Code
The above two Classes define two annotations, And the Checked annotation is an element of the Input annotation.
1 @ Target (value = {ElementType. FIELD}) 2 @ Retention (RetentionPolicy. RUNTIME) 3 public @ interface DateType {4 public String format () default "yyyy-MM-dd"; 5}View Code
This annotation is a date type annotation.
Define a Java bean using the three annotations defined above:
1 public class Person {2 3 @ Input (title = "no.") 4 private int id; 5 @ Input (title = "name") 6 private String name; 7 @ Input (title = "age") 8 private int age; 9 @ Input (title = "Date of Birth") 10 @ DateType (format = "yyyy-MM-dd HH: mm: ss ") 11 private Date birthday; 12 @ Input (title =" address ") 13 private String address; 14 @ Input (title =" gender ", checked = @ Checked (checked = true) 15 private String gender; 16 @ Input (title = "password", type = InputType. password) 17 private String password; 18 19 public String getGender () {20 return gender; 21} 22 public void setGender (String gender) {23 this. gender = gender; 24} 25 public int getId () {26 return id; 27} 28 public void setId (int id) {29 this. id = id; 30} 31 public String getName () {32 return name; 33} 34 public void setName (String name) {35 this. name = name; 36} 37 public int getAge () {38 return age; 39} 40 public void setAge (int age) {41 this. age = age; 42} 43 public Date getBirthday () {44 return birthday; 45} 46 public void setBirthday (Date birthday) {47 this. birthday = birthday; 48} 49 public String getAddress () {50 return address; 51} 52 public void setAddress (String address) {53 this. address = address; 54} 55 56 57}View Code
Define a Writer to process the preceding annotation ,:
1 package com. fjn. tools. annotation; 2 3 import java. io. printWriter; 4 import java. lang. reflect. field; 5 import java. util. date; 6/** 7 * packaging PrintWriter 8 */9 import com. fjn. tools. common. dateHelper; 10 @ SuppressWarnings ("rawtypes") 11 public class BeanWriter <T> extends PrintWriter {12 private PrintWriter out; 13 private T bean; 14 public BeanWriter (PrintWriter out) {15 super (out); 16 this. out = out; 17} 18 19 public void write () {20 if (bean = null) return; 21 22 Class clazz = bean. getClass (); 23 Field [] fields = clazz. getDeclaredFields (); 24 StringBuffer sbf = new StringBuffer (); 25 for (int I = 0; I <fields. length; I ++) {26 27 Input input = fields [I]. getAnnotation (Input. class); 28 if (input = null) continue; 29 fields [I]. setAccessible (true); 30 Object value = null; 31 // The date must be determined in the format of 32 if (fields [I]. getType () = Date. class | fields [I]. getType () = java. SQL. date. class) {33 value = getDateString (fields [I], bean); 34} else {35 try {36 value = fields [I]. get (bean); 37} catch (Exception e1) {38 e1.printStackTrace (); 39} 40} 41 42 sbf. append ("<input type = '") 43. append (input. type (). name () 44. append ("'name = '") 45. append (fields [I]. getName () 46. append ("'id = '") 47. append (input. id () 48. append ("'style = '") 49. append (input. style () 50. append ("'title = '") 51. append (input. title () 52. append ("'validator = '") 53. append (input. volidator () 54. append ("'class = '") 55. append (input. clazz () 56. append ("'value = '") 57. append (value ). 58 append ("'>"); 59 out. write (sbf. toString (); 60 out. flush (); 61 sbf. delete (0, sbf. length (); 62} 63} 64 65 private String getDateString (Field field, Object bean) {66 DateType dateType = field. getAnnotation (DateType. class); 67 String pattern = null; 68 if (dateType = null) {69 pattern = "yyyy-MM-dd"; 70} else 71 pattern = dateType. format (); 72 field. setAccessible (true); 73 String value = null; 74 try {75 value = DateHelper. parseString (Date) field. get (bean), pattern); 76} catch (IllegalArgumentException e) {77 // TODO Auto-generated catch block 78 e. printStackTrace (); 79} catch (IllegalAccessException e) {80 // TODO Auto-generated catch block 81 e. printStackTrace (); 82} 83 return value; 84} 85 86 public PrintWriter getOut () {87 return out; 88} 89 90 public void setOut (PrintWriter out) {91 this. out = out; 92} 93 94 public T getBean () {95 return bean; 96} 97 98 public void setBean (T bean) {99 this. bean = bean; 100} 101 102 103}View Code
The last test is:
Package com. fjn. tools. annotation. test; import java. io. printWriter; import java. util. date; import com. fjn. tools. annotation. beanWriter; import com. fjn. tools. annotation. person; public class InputTest {public static void main (String [] args) {Person person Person = new person (); Person. setAddress ("Haidian Xiyuan"); person. setAge (25); person. setName ("Zhang San"); person. setId (23); person. setBirthday (new Date (); BeanWriter <Person> inputWriter = new BeanWriter <Person> (new PrintWriter (System. out); inputWriter. setBean (person); inputWriter. write ();}}
If you are interested, you can execute the following code.
The above code execution result:
<Input type = 'text' name = 'id' id = ''style ='' title = 'number' validator = ''class ='' value = '23'> <input type = 'text' name = 'name' id = ''style ='' title = 'name' validator = ''class ='' value = 'zhang san'> <input type = 'text' name = 'age' id = ''style ='' title = ''validator ='' class = ''value = '25'> <input type = 'text' name = 'birthday' id = ''style ='' title = ''validator ='' class = ''value = '2017-07-17 12:26:15 '> <input type = 'text' name = 'address' id = ''style ='' title = ''validator ='' class = ''value = 'haidian Westcourt '>
For Java Annotation
You can also use reflection to obtain the package through this class.
To what extent can java se learn java web development?
What you have learned, exception handling, multithreading, generics, java io, but common class libraries, class sets, enumeration, reflection mechanisms, and annotation are all JAVA knowledge points. WEB development is not yet available. For WEB development. You also need to learn the following:
1: Database (there must be one)
2: Now I want to learn about Servlet and understand the operating mechanism, which can be replaced by Struts.
3: JDBC or Hibernate (connect to the database)
4: Html/JavaScript/Ajax (basic knowledge required)
5: JSP
6: Spring/EJB can be learned as needed. Generally, large companies will use these technologies.
7: Simply learn a type of server, such as Tomcat.
In this way, you can perform WEB development.