主要实现思路,自定义注解+反射
package com.zl.annotion;
import jdk.nashorn.internal.runtime.logging.Logger;
import java.lang.annotation.*;
import java.lang.reflect.Field;
/**
* @author zhanglei
* @description : 自定义注解实现简单ORM
* @date 2021/4/9$ 10:48$
* @return $
*/
public class ORMDemo {
public static void main(String[] args) throws ClassNotFoundException {
String table = "";
Class<?> aClass = Class.forName("com.zl.annotion.Student");
//获取类上指定注解
Table declaredAnnotation = aClass.getDeclaredAnnotation(Table.class);
table = declaredAnnotation.value();
Field[] declaredFields = aClass.getDeclaredFields();
StringBuffer sb = new StringBuffer("select ");
for(Field field : declaredFields){
//获取属性上指定注解
Property annotation = field.getAnnotation(Property.class);
String filed = annotation.filed();
sb.append(filed + ",");
}
String substring = sb.substring(0, sb.length() - 1);
System.out.println(substring+" from "+ table);
}
}
@Table("T_student")
@Logger
class Student{
@Property(filed = "id",length = 18)
String studentId;
@Property(filed = "name")
String studentName;
@Property(filed = "age")
int studentAge;
}
/**
* 只允许注解到类上(type:类,method:方法,filed:属性)
*/
@Target({ElementType.TYPE})
/**
* 表示需要在什么级别保存该注释信息,用于描述注解的生命周期(即:被描述的注解在什么范围内有效
*/
@Retention(RetentionPolicy.RUNTIME)
@interface Table{
String value();
}
@Target({ElementType.FIELD,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@interface Property{
String filed();
int length() default 999999999;
}
运行结果: select id,name,age from T_student
只需要在controller的方法上加上@sign注解,就会走到以下切面,做一个基本的验签功能
2.自定义注解+AOP的使用
package com.zl.projectone.aop;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Sign {
}
package com.zl.projectone.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
/**
* Created by wuwf on 17/4/27.
*/
@Component
@Aspect
public class SignAspect {
@Pointcut(value = "@annotation(com.zl.projectone.aop.Sign)")
public void sign() {
}
@Before("sign()")
public void deBefore(JoinPoint joinPoint) throws Throwable {
System.out.println("second before");
}
@Around("@annotation(sign)")
public Object around(ProceedingJoinPoint pjp, Sign sign) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
if(sign != null){
String signKey = request.getParameter("sign");
if("666".equals(signKey)){
try {
return pjp.proceed();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}else{
return "验签失败";
}
}else{
try {
return pjp.proceed();
} catch (Throwable throwable) {
throwable.printStackTrace();
return null;
}
}
return null;
}
}