Java注解编程

一、Java注解编程

在开发过程中我们会看到,继承接口会出现@Override注解,有时候还会提示写注解@SuppressWarnings
还有在使用Spring,Mybatis,Struts2等框架时,常常会使用注解,很多人都知道这些怎么使用,但是不知道他是怎么来的。其实,这是Java的特性

通过学习注解编程我们可以

1.能够读懂别人写的代码,特别是框架相关的代码
2.编程更加简洁,代码更加清晰

Java注解是一种代码级别的说明。它是JDK1.5及以后版本引入的一个特性,与类、接口、枚举是在同一个层次。它可以声明在包、类、字段、方法、局部变量、方法参数等的前面,用来对这些元素进行说明,注释。

二、注解的分类

这里引用网友的一张图:http://www.cnblogs.com/xing901022/p/3966799.html

上图分类可以分为三类:注解大体上分为三种:标记注解,一般注解,元注解,也可以按照如下机制分类

2.1按照运机制分为
源码注解
编译时的注解
运行时的注解

源码注解:注解只在源码存在,编译成.class文件就不存在了
编译时注解:注解在源码和.class中都存在 (如@Override,@Deprecated,@Suppvisewarnings)
运行时注解:在运行阶段还会起作用,甚至会影响运行逻辑的注解(@Autowired)

2.2按照注解的来源
来自JDK的注解
来自第三方的注解
自定义的注解
2.3元注解
给注解进行的注解

三、常见的注解实例

@Override  //实现类实现接口的时候常常会见到,说明覆盖了父类或接口中的方法
@Deprecated //声明某个方法过时了
@Suppvisewarnings //忽略警告,抑制编译器发出特定的警告
@Override标识覆盖父类或父接口中的方法
实现接口或继承父类的时候,方法上就会有@Override,表示覆盖父类,或父接口中的方法
@Deprecated 标注某个方法过时了,不建议使用

@Suppvisewarnings 忽略警告,抑制编译器发出特定的警告

在我们调用过时的方法或其他情况下我们会看到编译器给我们发出告警

我们可以使用@Suppvisewarnings("抑制那种告警的注解名"),如下,告警消失了

还有一些常见的第三方框架的注解

Spring    @Autowired  @Service  @Repository  
Mybatis   @InserProvider @UpdateProvier  @Options


四、自定义注解

我们主要了解如下内容:
自定义注解的语法要求
注解的注解(元注解)
使用自定义注解
解析注解

4.1自定义注解的语法要求
自定义注解有如下要求:
1.成员类型是受限制的,合法的类型包括原始类型(基本数据类型)以及String,Class,Annotaion,Enumeration
2.注解可以设置默认值,如上图 defalut 18
3.如果只有个注解成员,成员应该为 String value();  //标准是这样建议的
4.注解成员必须是无参无异常方式声明
5.注解类可以没有成员,没有成员的注解称为标识注解

4.2注解的注解(元注解)
上图中的注解类中的注解都是元注解
@Target : 表示该注解的作用范围,可能的值在枚举类 ElemenetType 中,包括: 
          ElemenetType.CONSTRUCTOR        构造器声明 
          ElemenetType.FIELD            域声明(包括 enum 实例) 
          ElemenetType.LOCAL_VARIABLE     局部变量声明 
          ElemenetType.METHOD            方法声明 
          ElemenetType.PACKAGE           包声明 
          ElemenetType.PARAMETER          参数声明 
          ElemenetType.TYPE             类,接口(包括注解类型)或enum声明
@Retention : 表示在什么级别保存该注解信息。可选的参数值在枚举类型 RetentionPolicy 中,包括: 
          RetentionPolicy.SOURCE        注解将被编译器丢弃 
          RetentionPolicy.CLASS       注解在class文件中可用,但会被VM丢弃 
          RetentionPolicy.RUNTIME      VM将在运行期也保留注释,因此可以通过反射机制读取注解的信息。
@Documented : 将此注解包含在 javadoc 中 ,它代表着此注解会被javadoc工具提取成文档。在doc文档中的内容会因为此注解的信息内容不同而不同。
@Inherited :注解类中使用该注解标识,如果类中使用了该注解,子类会继承类中(只有在继承的时候生效,而且只会继承类中的注解,方法或属性中的注解不会生效)的注解

五、注解的解析

概念:通过反射获取类,函数或成员上的运行时的注解信息,从而实现动态控制程序运行的逻辑

自定义注解类:


 
 
  1. @Target({ElementType.METHOD,ElementType.TYPE})
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Inherited
  4. @Documented
  5. public @interface Description {
  6. String value();
  7. }

Person.java


 
 
  1. package com.ann.test;
  2. @Description("I am a class annotation")
  3. public class Person {
  4. @Description("I am a method annotation")
  5. public String name() {
  6. return null;
  7. }
  8. @Description("I am a method annotation")
  9. public int age(){
  10. return 0;
  11. }
  12. @Deprecated //注解说明该方法过时了
  13. public void sing(){
  14. }
  15. }
Child.java,继承Person,验证@Inherited


 
 
  1. package com.ann.test;
  2. public class Child extends Person {
  3. @Override
  4. public String name() {
  5. return null;
  6. }
  7. @Override
  8. public int age() {
  9. return 0;
  10. }
  11. @Override
  12. public void sing() {
  13. }
  14. }


ParseAnn.java解析注解,主要通过反射解析


 
 
  1. package com.ann.test;
  2. import java.lang.annotation.Annotation;
  3. import java.lang.reflect.Method;
  4. /*
  5. * 通过反射解析注解
  6. * */
  7. public class ParseAnn {
  8. public static void main(String[] args) {
  9. //1.使用类加载器加载类
  10. try{
  11. Class c = Class.forName( "com.ann.test.Child");
  12. //2.找到类上边的注解
  13. boolean isExist = c.isAnnotationPresent(Description.class);
  14. if(isExist){
  15. // 3.拿到注解实例
  16. Description description = (Description) c.getAnnotation(Description.class);
  17. System.out.println(description.value()+"--- 1 ");
  18. }
  19. //4.找到方法上的注解
  20. Method[] methods =c.getMethods();
  21. for(Method method:methods){
  22. boolean isMExist = method.isAnnotationPresent(Description.class);
  23. if(isMExist){
  24. //5.拿到方法上的注解实例
  25. Description dmethod = method.getAnnotation(Description.class);
  26. System.out.println(dmethod.value()+"--- 2 ");
  27. }
  28. }
  29. //获取所有的方法上的注解
  30. for(Method m:methods){
  31. Annotation[] annotations = m.getAnnotations();
  32. for(Annotation annotation:annotations){
  33. if(annotation instanceof Description){
  34. Description d = (Description)annotation;
  35. System.out.println(d.value()+"--- 3 ");
  36. }
  37. }
  38. }
  39. }catch(ClassNotFoundException e){
  40. e.printStackTrace();
  41. }
  42. }
  43. }

结果:

I am a class annotation---1

从结果可以看出,只有类上的注解被继承类,通过反实现一个仿照Hibernate的解决方案,核心代码通过注解实现

六、仿照Hibernate自定义注解实现

需求:1.有一张用户表,字段包括用户ID,用户名,昵称,年龄,性别,所在城市,邮箱,手机号。
2.为方便对每个字段的组合条件进行检索,并打印出SQL
3.使用方式足够简单射可以很方便的解析出注解

自定义注解类:用于注解表Table


 
 
  1. package com.ann.test2;
  2. import java.lang.annotation.ElementType;
  3. import java.lang.annotation.Retention;
  4. import java.lang.annotation.RetentionPolicy;
  5. import java.lang.annotation.Target;
  6. @Target({ElementType.TYPE})
  7. @Retention(RetentionPolicy.RUNTIME)
  8. public @interface Table {
  9. String value();
  10. }
自定义注解类:用于注解Column

 
 
  1. import java.lang.annotation.ElementType;
  2. import java.lang.annotation.Retention;
  3. import java.lang.annotation.RetentionPolicy;
  4. import java.lang.annotation.Target;
  5. @Target({ElementType.FIELD})
  6. @Retention(RetentionPolicy.RUNTIME)
  7. public @interface Column {
  8. String value();
  9. }

注解的使用:


 
 
  1. package com.ann.test2;
  2. @Table("user")
  3. public class Filter {
  4. @Column("id")
  5. private int id;
  6. @Column("user_name")
  7. private String userName;
  8. @Column("nick_name")
  9. private String nickName;
  10. @Column("age")
  11. private int age;
  12. @Column("city")
  13. private String city;
  14. @Column("email")
  15. private String email;
  16. @Column("mobile")
  17. private String mobile;
  18. public int getId() {
  19. return id;
  20. }
  21. public void setId(int id) {
  22. this.id = id;
  23. }
  24. public String getUserName() {
  25. return userName;
  26. }
  27. public void setUserName(String userName) {
  28. this.userName = userName;
  29. }
  30. public String getNickName() {
  31. return nickName;
  32. }
  33. public void setNickName(String nickName) {
  34. this.nickName = nickName;
  35. }
  36. public int getAge() {
  37. return age;
  38. }
  39. public void setAge(int age) {
  40. this.age = age;
  41. }
  42. public String getCity() {
  43. return city;
  44. }
  45. public void setCity(String city) {
  46. this.city = city;
  47. }
  48. public String getEmail() {
  49. return email;
  50. }
  51. public void setEmail(String email) {
  52. this.email = email;
  53. }
  54. public String getMobile() {
  55. return mobile;
  56. }
  57. public void setMobile(String mobile) {
  58. this.mobile = mobile;
  59. }
  60. }


注解解析成sql


 
 
  1. package com.ann.test2;
  2. import java.io.File;
  3. import java.lang.reflect.Field;
  4. import java.lang.reflect.InvocationTargetException;
  5. import java.lang.reflect.Method;
  6. public class Test {
  7. public static void main(String[] args) {
  8. Filter filter = new Filter();
  9. filter.setId( 10); //查询id为10的用户
  10. Filter filter2 = new Filter();
  11. filter2.setUserName( "lucy"); //查询用户名为lucy的用户
  12. Filter filter3 = new Filter();
  13. filter3.setEmail( "liu@sina.com,zh@163.com,88888@qq.com");
  14. String sql1= query(filter);
  15. String sql2= query(filter2);
  16. String sql3= query(filter3);
  17. System.out.println(sql1);
  18. System.out.println(sql2);
  19. System.out.println(sql3);
  20. }
  21. private static String query(Filter filter){
  22. StringBuilder sb = new StringBuilder();
  23. //1.获取class
  24. Class c = filter.getClass();
  25. //2.获取到Table的名字
  26. boolean exists = c.isAnnotationPresent(Table.class);
  27. if(!exists){
  28. return null;
  29. }
  30. Table table = (Table) c.getAnnotation(Table.class);
  31. String tableName = table.value();
  32. sb.append("select * form ").append(tableName).append(" where 1= 1 ");
  33. //3.遍历所有的字段
  34. Field[] fArray = c.getDeclaredFields();
  35. for(Field f:fArray){
  36. //4.处理每个字段对应的sql
  37. //4.1拿到字段的名字
  38. boolean fExists = f.isAnnotationPresent(Column.class);
  39. if(!fExists){
  40. continue;
  41. }
  42. Column column = f.getAnnotation(Column.class);
  43. //4.2拿到字段的值
  44. String columnName = column.value();
  45. //获取属性的值
  46. String filedName = f.getName();
  47. String getMethodName = "get "+filedName.substring(0, 1).toUpperCase()+filedName.substring(1);
  48. try {
  49. Method method = c.getMethod(getMethodName);
  50. Object fileValue =method.invoke(filter);
  51. //4.3拼接sql
  52. if(fileValue==null || (fileValue instanceof Integer &&(Integer)fileValue==0)){
  53. continue;
  54. }
  55. sb.append(" and ").append(filedName);
  56. if(fileValue instanceof String){// in()
  57. if(((String) fileValue).contains(", ")){
  58. String[] values = ((String)fileValue).split(", ");
  59. sb.append(" in ( ");
  60. for(String v:values){
  61. sb.append(" '").append(v).append("' ").append(", ");
  62. }
  63. sb.deleteCharAt(sb.length()-1);
  64. sb.append(") ");
  65. }else{
  66. sb.append("= ").append(" '").append(fileValue).append("' ");
  67. }
  68. }else if(fileValue instanceof Integer){
  69. sb.append("= ").append(fileValue);
  70. }
  71. } catch (Exception e) {
  72. e.printStackTrace();
  73. }
  74. }
  75. return sb.toString();
  76. }
  77. }

结果:

select * form user where 1=1  and id=10
select * form user where 1=1  and userName='lucy'
select * form user where 1=1  and email in ('liu@sina.com','zh@163.com','88888@qq.com')


还可以参见博客:http://www.cnblogs.com/pepcod/archive/2013/02/20/2918719.html


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值