spring aop使用说明
?????????
?????????????????
????????????????????????????????????
相关注解
????????
@Aspect:声明一个切面
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE}) //标注在类上
public @interface Aspect {
String value() default "";
}
??????
@Before:前置通知,在方法调用前执行
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD}) //标注在方法上
public @interface Before {
String value();
String argNames() default "";
}
?????????
@After:后置通知,在方法调用后执行
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD}) //标注在方法上
public @interface After {
String value();
String argNames() default "";
}
????????
@AfterReturing:方法正常返回时调用
Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD}) //标注在方法上
public @interface AfterReturning {
String value() default "";
String pointcut() default "";
String returning() default "";
String argNames() default "";
}
??????????
@AfterThrowing:方法抛出异常时调用
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD}) //标注在方法上
public @interface AfterThrowing {
String value() default "";
String pointcut() default "";
String throwing() default "";
String argNames() default "";
}
????????
@Around:环绕通知,可实现前面通知功能,需触发方法执行
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface Around {
String value();
String argNames() default "";
}
???????????
??????????????
????????????????????????????????????
相关类与接口
?????????
JoinPoint:连接点
public interface JoinPoint {
String METHOD_EXECUTION = "method-execution";
String METHOD_CALL = "method-call";
String CONSTRUCTOR_EXECUTION = "constructor-execution";
String CONSTRUCTOR_CALL = "constructor-call";
String FIELD_GET = "field-get";
String FIELD_SET = "field-set";
String STATICINITIALIZATION = "staticinitialization";
String PREINITIALIZATION = "preinitialization";
String INITIALIZATION = "initialization";
String EXCEPTION_HANDLER = "exception-handler";
String SYNCHRONIZATION_LOCK = "lock";
String SYNCHRONIZATION_UNLOCK = "unlock";
String ADVICE_EXECUTION = "adviceexecution";
String toString();
String toShortString();
String toLongString();
Object getThis(); //获取当前对象
Object getTarget(); //获取目标对象
Object[] getArgs(); //获取参数体
Signature getSignature(); //获取签名信息(可回去方法签名)
SourceLocation getSourceLocation();
String getKind();
JoinPoint.StaticPart getStaticPart();
public interface EnclosingStaticPart extends JoinPoint.StaticPart {
}
public interface StaticPart {
int getId();
String getKind();
Signature getSignature();
SourceLocation getSourceLocation();
String toString();
String toShortString();
String toLongString();
}
}
?????????
ProceedingJoinPoint:连接点,环绕通知使用
public interface ProceedingJoinPoint extends JoinPoint {
void set$AroundClosure(AroundClosure var1);
default void stack$AroundClosure(AroundClosure arc) {
throw new UnsupportedOperationException();
}
Object proceed() throws Throwable;
Object proceed(Object[] var1) throws Throwable;
}
?????????
Signature:方法签名接口
public interface Signature {
String toString();
String toShortString();
String toLongString();
String getName();
int getModifiers();
Class getDeclaringType();
String getDeclaringTypeName();
}
?????????????
CodeSignature
public interface CodeSignature extends MemberSignature {
Class[] getParameterTypes(); //获取参数类型
String[] getParameterNames(); //获取参数名称
Class[] getExceptionTypes(); //获取方法抛出的异常类型数组
}
??????????
MethodSignature
public interface MethodSignature extends CodeSignature {
Class getReturnType(); //获取方法返回类型
Method getMethod(); //获取切点方法
}
???????
Method
public final class Method extends Executable {
private Class<?> clazz;
private int slot;
private String name;
private Class<?> returnType;
private Class<?>[] parameterTypes;
private Class<?>[] exceptionTypes;
private int modifiers;
private transient String signature;
private transient MethodRepository genericInfo;
private byte[] annotations;
private byte[] parameterAnnotations;
private byte[] annotationDefault;
private Method root;
private volatile MethodAccessor methodAccessor;
Method(Class<?> declaringClass,
String name,
Class<?>[] parameterTypes,
Class<?> returnType,
Class<?>[] checkedExceptions,
int modifiers,
int slot,
String signature,
byte[] annotations,
byte[] parameterAnnotations,
byte[] annotationDefault) {
Method copy() {
public Class<?> getDeclaringClass() { //方法所在的类
public String getName() {
public int getModifiers() {
public Object getDefaultValue() {
public Class<?> getReturnType() {
public Type getGenericReturnType() {
public AnnotatedType getAnnotatedReturnType() {
public int getParameterCount() {
public Class<?>[] getParameterTypes() {
public Type[] getGenericParameterTypes() {
public TypeVariable<Method>[] getTypeParameters() {
public Class<?>[] getExceptionTypes() {
public Type[] getGenericExceptionTypes() {
public Object invoke(Object obj, Object... args)
throws IllegalAccessException, IllegalArgumentException,
InvocationTargetException
{
public Annotation[] getDeclaredAnnotations() {
public Annotation[][] getParameterAnnotations() {
public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
public boolean isBridge() {
public boolean isVarArgs() {
public boolean isSynthetic() {
public boolean isDefault() {
public boolean equals(Object obj) {
public int hashCode() {
public String toString() {
public String toGenericString() {
void specificToStringHeader(StringBuilder sb) {
void specificToGenericStringHeader(StringBuilder sb) {
void handleParameterNumberMismatch(int resultLength, int numParameters) {
void setMethodAccessor(MethodAccessor accessor) {
boolean hasGenericInformation() {
Executable getRoot() {
byte[] getAnnotationBytes() {
MethodAccessor getMethodAccessor() {
MethodRepository getGenericInfo() {
private String getGenericSignature() {
private GenericsFactory getFactory() {
private MethodAccessor acquireMethodAccessor() {
?????????????
??????????????????
????????????????????????????????????
反射工具类
?????????
AopUtils
public abstract class AopUtils {
public AopUtils() {
}
public static boolean isAopProxy(@Nullable Object object) { //是否是aop代理(cglib代理或者jdk代理)
public static boolean isJdkDynamicProxy(@Nullable Object object) { //是否是jk代理
public static boolean isCglibProxy(@Nullable Object object) { //是否是cglib代理
public static Class<?> getTargetClass(Object candidate) { //获取目标对象类(被代理类)
public static Object invokeJoinpointUsingReflection(@Nullable Object target, Method method, Object[] args) throws Throwable {
//触发方法调用
public static Method selectInvocableMethod(Method method, @Nullable Class<?> targetType) {
public static boolean isEqualsMethod(@Nullable Method method) {
public static boolean isHashCodeMethod(@Nullable Method method) {
public static boolean isToStringMethod(@Nullable Method method) {
public static boolean isFinalizeMethod(@Nullable Method method) {
public static Method getMostSpecificMethod(Method method, @Nullable Class<?> targetClass) {
public static boolean canApply(Pointcut pc, Class<?> targetClass) {
public static boolean canApply(Pointcut pc, Class<?> targetClass, boolean hasIntroductions) {
public static boolean canApply(Advisor advisor, Class<?> targetClass) {
public static boolean canApply(Advisor advisor, Class<?> targetClass, boolean hasIntroductions) {
public static List<Advisor> findAdvisorsThatCanApply(List<Advisor> candidateAdvisors, Class<?> clazz) {
????????????
ReflectionUtils
public abstract class ReflectionUtils {
public static final ReflectionUtils.MethodFilter USER_DECLARED_METHODS = (method) -> {
return !method.isBridge() && !method.isSynthetic() && method.getDeclaringClass() != Object.class;
};
public static final ReflectionUtils.FieldFilter COPYABLE_FIELDS = (field) -> {
return !Modifier.isStatic(field.getModifiers()) && !Modifier.isFinal(field.getModifiers());
};
private static final String CGLIB_RENAMED_METHOD_PREFIX = "CGLIB$";
private static final Class<?>[] EMPTY_CLASS_ARRAY = new Class[0];
private static final Method[] EMPTY_METHOD_ARRAY = new Method[0];
private static final Field[] EMPTY_FIELD_ARRAY = new Field[0];
private static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
private static final Map<Class<?>, Method[]> declaredMethodsCache = new ConcurrentReferenceHashMap(256);
private static final Map<Class<?>, Field[]> declaredFieldsCache = new ConcurrentReferenceHashMap(256);
public ReflectionUtils() {
}
public static void handleReflectionException(Exception ex) {
public static void handleInvocationTargetException(InvocationTargetException ex) {
public static void rethrowRuntimeException(Throwable ex) {
public static void rethrowException(Throwable ex) throws Exception {
public static <T> Constructor<T> accessibleConstructor(Class<T> clazz, Class<?>... parameterTypes) throws NoSuchMethodException {
public static void makeAccessible(Constructor<?> ctor) {
*********
method操作
public static void makeAccessible(Method method) {
public static Method findMethod(Class<?> clazz, String name) {
public static Method findMethod(Class<?> clazz, String name, @Nullable Class<?>... paramTypes) {
public static Object invokeMethod(Method method, @Nullable Object target) {
public static Object invokeMethod(Method method, @Nullable Object target, @Nullable Object... args) {
public static void doWithLocalMethods(Class<?> clazz, ReflectionUtils.MethodCallback mc) {
public static void doWithMethods(Class<?> clazz, ReflectionUtils.MethodCallback mc) {
public static void doWithMethods(Class<?> clazz, ReflectionUtils.MethodCallback mc, @Nullable ReflectionUtils.MethodFilter mf) {
public static Method[] getDeclaredMethods(Class<?> clazz) {
public static Method[] getAllDeclaredMethods(Class<?> leafClass) {
public static Method[] getUniqueDeclaredMethods(Class<?> leafClass) {
public static Method[] getUniqueDeclaredMethods(Class<?> leafClass, @Nullable ReflectionUtils.MethodFilter mf) {
public static boolean isEqualsMethod(@Nullable Method method) {
public static boolean isHashCodeMethod(@Nullable Method method) {
public static boolean isToStringMethod(@Nullable Method method) {
public static boolean isObjectMethod(@Nullable Method method) {
public static boolean isCglibRenamedMethod(Method renamedMethod) {
public static boolean declaresException(Method method, Class<?> exceptionType) {
private static boolean hasSameParams(Method method, Class<?>[] paramTypes) {
private static Method[] getDeclaredMethods(Class<?> clazz, boolean defensive) {
private static List<Method> findConcreteMethodsOnInterfaces(Class<?> clazz) {
*********
field操作
public static void makeAccessible(Field field) {
public static Field findField(Class<?> clazz, String name) {
public static Field findField(Class<?> clazz, @Nullable String name, @Nullable Class<?> type) {
public static void setField(Field field, @Nullable Object target, @Nullable Object value) {
public static Object getField(Field field, @Nullable Object target) {
public static void doWithLocalFields(Class<?> clazz, ReflectionUtils.FieldCallback fc) {
public static void doWithFields(Class<?> clazz, ReflectionUtils.FieldCallback fc) {
public static void doWithFields(Class<?> clazz, ReflectionUtils.FieldCallback fc, @Nullable ReflectionUtils.FieldFilter ff) {
public static boolean isPublicStaticFinal(Field field) {
public static void shallowCopyFieldState(final Object src, final Object dest) {
private static Field[] getDeclaredFields(Class<?> clazz) {
public static void clearCache() {
declaredMethodsCache.clear();
declaredFieldsCache.clear();
}
*********
内部接口:FieldFilter
@FunctionalInterface
public interface FieldFilter {
boolean matches(Field field);
default ReflectionUtils.FieldFilter and(ReflectionUtils.FieldFilter next) {
Assert.notNull(next, "Next FieldFilter must not be null");
return (field) -> {
return this.matches(field) && next.matches(field);
};
}
}
*********
内部接口:FieldCallback
@FunctionalInterface
public interface FieldCallback {
void doWith(Field field) throws IllegalArgumentException, IllegalAccessException;
}
*********
内部接口:MethodFilter
@FunctionalInterface
public interface MethodFilter {
boolean matches(Method method);
default ReflectionUtils.MethodFilter and(ReflectionUtils.MethodFilter next) {
Assert.notNull(next, "Next MethodFilter must not be null");
return (method) -> {
return this.matches(method) && next.matches(method);
};
}
}
*********
内部接口:MethodCallback
@FunctionalInterface
public interface MethodCallback {
void doWith(Method method) throws IllegalArgumentException, IllegalAccessException;
}
}
????????
??????????????????
|