Java注解-自定义注解实例

在Java中注解随处可见,学习Java注解,知道其原理,可以读懂很多开源框架,如Spring,Mybatis等,还可以自定义注解实现更高级的功能。 

 

一、常见的Java注解

 Jdk自带的注解:@Override,@SuppressWarnings,@Deprecated(方法过时)

 第三方框架注解:Spring,Mybatis等

 

二、注解的分类

1.按运行机制分

 源码注解     源码存在,class文件不存在

 编译时注解  源码,class文件存在

 运行时注解  spring @antuAire

2.按来源分

 Jdk自带的注解

 第三方注解

 自定义注解

3.元注解

 给注解用的注解

 

三、注解语法

1.声明public @interface

2.成员以无参无异常方式声明

3.可以用default为成员指定一个默认值

    int age() default 18;

4.成员的返回值类型是有限制的,合法的有基本数据类型,String,Class,Annotation,Enurmeration

5.如果注解只有一个成员,则成员名必须为value(),在使用时可以忽略成员名和赋值号(=)

6.注解类可以没有成员,没有成员的注解为标识注解

 

元注解

作用于注解上的注解,如@Target,@Retention,@Inherited,@Documented

package com.yuwl.ann;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 自定义注解
 * @author Yuwl
 */
@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Description {
	
	String value();

}

 Target注解:注解的作用域,用在哪个地方,包含Java的所有元素:

 CONSTRUCTOR 构造方法

 FiELD 字段

 LOCAL_VERIABLE 局部变量

 METHOD 方法

 PACKAGE 包

 TYPE 类接口

 

Retention注解:生命周期,包含:

SOURCE 源码

CLASS 编译

RUNNTIME 运行时

 

Inheriter注解:标识性元注解,允许子类继承,但只适用于类的继承,不能用于接口继承,而且只会继承类的注解,不会继承方法的

 

 

Documented注解:生成javadoc时会包含注解

 

注解的使用:

@注解名(成员名1=成员值1,成员名2=成员值2)

 

四、自定义注解

1.自定义注解

package com.yuwl.ann;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 自定义注解
 * @author Yuwl
 */
@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Description {
	
	String value();

}

2.注解的使用

package com.yuwl.ann;

/**
 * 自定义注解的使用
 * @author Yuwl
 */
@Description("I am class annotation")
public class UseAnnotation {
	
	@Description("I am method annotation")
	public void hello(){
		
	}

}

3.注解的解析

package com.yuwl.ann;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
/**
 * 解析注解
 * @author Yuwl
 */
public class ParseAnnotation {

	public static void main(String[] args) {
		try {
			//1.使用类加载器加载类
			Class c = Class.forName("com.yuwl.ann.UseAnnotation");
			//2.找到类上的注解
			boolean exist = c.isAnnotationPresent(Description.class);
			if(exist){
				//3.拿到注解实例
				Description d = (Description)c.getAnnotation(Description.class);
				System.out.println(d.value());
			}
			
			//4.找到方法上的注解
			Method[] ms = c.getMethods();
			for(Method m : ms){
				if(m.isAnnotationPresent(Description.class)){
					Description d = (Description)m.getAnnotation(Description.class);
					System.out.println(d.value());
				}
			}
			//5.方法注解的另一种解析方式
			for(Method m : ms){
				Annotation[] ans = m.getAnnotations();
				for(Annotation an : ans){
					Description d = (Description)an;
					System.out.println(d.value());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

 注解的解析主要用到Java的反射。

 

五、模拟实体到数据库表字段的映射

1.自定义表,字段注解

package com.yuwl.ann.dao;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
 * 表自定义注解
 * @author Yuwl
 */
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Table {

	String value();
}

 

package com.yuwl.ann.dao;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
 * 字段自定义注解
 * @author Yuwl
 */
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Column {

	String value();
}

 2.实体使用注解

package com.yuwl.ann.dao;
/**
 * 用户实体使用注解
 * @author Yuwl
 */
@Table("user")
public class User {
	
	@Column("id")
	private int id;
	
	@Column("userName")
	private String userName;
	
	@Column("sex")
	private int sex;
	
	@Column("mobile")
	private String mobile;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public int getSex() {
		return sex;
	}

	public void setSex(int sex) {
		this.sex = sex;
	}


	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}
	

}

 3.测试

package com.yuwl.ann.dao;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
 * 模拟实体到数据库表字段的测试
 * @author Yuwl
 */
public class Test {

	public static void main(String[] args) {
		User u = new User();
		u.setUserName("张三");
		u.setSex(1);
		System.out.println(parseUser(u));
	}
	
	public static String parseUser(User u){
		StringBuffer sb = new StringBuffer();
		sb.append("select * from ");
		try {
			//1.获取表名
			Class c = Class.forName("com.yuwl.ann.dao.User");
			if(c.isAnnotationPresent(Table.class)){
				Table t = (Table)c.getAnnotation(Table.class);
				String tableName = t.value();
				sb.append(tableName);
			}
			sb.append(" where 1=1");
			//2.获取字段名与值
			Field[] fs = c.getDeclaredFields();
			for(Field f : fs){
				//2.1字段名
				String column = "";
				if(f.isAnnotationPresent(Column.class)){
					Column fld = (Column)f.getAnnotation(Column.class);
					column = fld.value();
				}
				//2.2字段值
				String fieldName = f.getName();
				String getMethod = "get"+fieldName.substring(0, 1).toUpperCase()+fieldName.substring(1);
				Method method = c.getMethod(getMethod);
				Object fieldValue = method.invoke(u);
				if(fieldValue instanceof Integer && (Integer)fieldValue == 0){
					continue;
				}
				if(fieldValue != null){
					sb.append(" and ").append(column).append("=");
					if(fieldValue instanceof String){
						sb.append("'").append(fieldValue).append("'");
					}else{
						sb.append(fieldValue);
					}
					
				}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sb.toString();
	}
}

 效果:

select * from user where 1=1 and userName='张三' and sex=1

 

总结

Java注解不复杂,主要也就这么多东西,知道其实现原理,如何自定义注解,就能读懂别人写的注解,自己也能写了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值