一、Springboot中定义注解
1,定义注解类
/**
* 自定义日志打印
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyLogger {
String value() default "";
}
2,定义AOP切面
/**
* 采用aop打印日志
*/
@Component
@Aspect
@Slf4j
public class MyLoggerAspect {
/**
* 其中@Pointcut声明了切点(这里的切点是我们自定义的注解类),
* @Before声明了通知内容,在具体的通知中,我们通过@annotation(logger)拿到了自定义的注解对象, 所以就能够获取我们在使用注解时赋予的值了。
*/
@Pointcut("@annotation(com.taiji.xhwcb.system.config.ann.MyLogger)")
private void pointcut() {
}
@Before("pointcut() && @annotation(myLogger)")
public void before(JoinPoint joinPoint, MyLogger myLogger) {
//类名
String className = joinPoint.getSignature().getDeclaringType().getSimpleName();
//方法名
String methodName = joinPoint.getSignature().getName();
//获取所有的参数
Object[] args = joinPoint.getArgs();
StringBuffer sb = new StringBuffer();
sb.append("类名:").append(className).append(" - ").append("方法名:").append(methodName);
for (int i = 0; i < args.length; i++) {
sb.append(" - ").append("参数" + (i+1) + ":").append(args[i]);
}
//登陆用户
TaijiUser user = SecurityUtils.getUser();
if (!EmptyUtil.isEmpty(user)) {
sb.append(" - ").append("用户为:").append(user.getId()).append("-").append(user.getUsername());
}
log.info(sb.toString());
}
}
3,在使用的方法上加上日志
@MyLogger
@ApiOperation(value = "分页查询", notes = "分页查询")
@GetMapping("/page" )
public R getPlayerConfigPage(Page page, PlayerConfig playerConfig) {}
4,打印效果
二、普通类中注解的使用
1,定义注解
//table表注解
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface TableRef {
String value();
}
//主键注解
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Rowkey {
}
//普通字段注解
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Column {
String family() default "info";
String column() default "";
}
2,使用注解
@Data
@TableRef("ct:calllog")
public class Calllog {
@Rowkey
private String rowkey;
@Column(family = "caller")
private String call1;
@Column(family = "caller")
private String call2;
@Column(family = "caller")
private String calltime;
@Column(family = "caller")
private String duration;
@Column(family = "caller")
private String flag = "1";
public Calllog() {
}
public Calllog(String data ) {
String[] values = data.split(" ");
call1 = values[0];
call2 = values[1];
calltime = values[2];
duration = values[3];
}
}
3,注解生效
protected void putData(Object obj) throws Exception {
//反射
Class clazz = obj.getClass();
TableRef tableRef = (TableRef)clazz.getAnnotation(TableRef.class);
String tableName = tableRef.value();
//获取所有的字段
Field[] fs = clazz.getDeclaredFields();
String stringRowkey = "";
for (Field f : fs) {
Rowkey rowkey = f.getAnnotation(Rowkey.class);
if ( rowkey != null ) {
f.setAccessible(true);
//获取属性值
stringRowkey = (String)f.get(obj);
break;
}
}
Connection conn = getConnection();
Table table = conn.getTable(TableName.valueOf(tableName));
Put put = new Put(Bytes.toBytes(stringRowkey));
for (Field f : fs) {
Column column = f.getAnnotation(Column.class);
if (column != null) {
String family = column.family();
String colName = column.column();
if ( colName == null || "".equals(colName) ) {
//获取属性名
colName = f.getName();
}
f.setAccessible(true);
//获取属性值
String value = (String)f.get(obj);
put.addColumn(Bytes.toBytes(family), Bytes.toBytes(colName), Bytes.toBytes(value));
}
}
// 增加数据
table.put(put);
// 关闭表
table.close();
}