spring中自定义注解(annotation)与AOP中获取注解
2015-04-15 16:51
525 查看
一、自定义注解(annotation)
自定义注解的作用:在反射中获取注解,以取得注解修饰的类、方法或属性的相关解释。
package me.lichunlong.spring.annotation;
import java.lang.annotation.Documented;
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)
@Documented
public @interface LogAnnotation {
//自定义注解的属性,default是设置默认值
String desc() default "无描述信息";
}
二、自定义注解的使用
package me.lichunlong.spring.service;
import me.lichunlong.spring.annotation.LogAnnotation;
import me.lichunlong.spring.jdbc.JdbcUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
//与其它注解一样的使用
@LogAnnotation(desc="this is UserService")
public void add() {
System.out.println("UserService add...");
}
}
三、AOP中获取注解
// 环绕通知:类似与动态代理的全过程
// 携带参数ProceedingJoinPoint,且必须有返回值,即目标方法的返回
@Around(value = "execution(* me.lichunlong.spring.service.*.*(..)) &&
@annotation(log)")
public Object aroundMethod(ProceedingJoinPoint pjd,
LogAnnotation log) {
Object result = null;
System.out.println(log.desc());
try {
System.out.println("前置通知");
result = pjd.proceed();
System.out.println("后置通知");
} catch (Throwable e) {
System.out.println("异常通知");
}
System.out.println("返回通知");
return result;
}
自定义注解的作用:在反射中获取注解,以取得注解修饰的类、方法或属性的相关解释。
package me.lichunlong.spring.annotation;
import java.lang.annotation.Documented;
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)
@Documented
public @interface LogAnnotation {
//自定义注解的属性,default是设置默认值
String desc() default "无描述信息";
}
二、自定义注解的使用
package me.lichunlong.spring.service;
import me.lichunlong.spring.annotation.LogAnnotation;
import me.lichunlong.spring.jdbc.JdbcUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
//与其它注解一样的使用
@LogAnnotation(desc="this is UserService")
public void add() {
System.out.println("UserService add...");
}
}
三、AOP中获取注解
// 环绕通知:类似与动态代理的全过程
// 携带参数ProceedingJoinPoint,且必须有返回值,即目标方法的返回
@Around(value = "execution(* me.lichunlong.spring.service.*.*(..)) &&
@annotation(log)")
public Object aroundMethod(ProceedingJoinPoint pjd,
LogAnnotation log) {
Object result = null;
System.out.println(log.desc());
try {
System.out.println("前置通知");
result = pjd.proceed();
System.out.println("后置通知");
} catch (Throwable e) {
System.out.println("异常通知");
}
System.out.println("返回通知");
return result;
}
相关文章推荐
- spring中自定义注解(annotation)与AOP中获取注解
- 在spring的aop中获取自定义注解的参数值,即在切面中获取annotation的参数值
- spring中自定义注解(annotation)与AOP中获取注解
- spring中自定义注解(annotation)与获取注解
- Spring AOP 自定义注解获取http接口及WebService接口入参和出参
- Springboot 自定义注解 AOP切面获取操作日志
- Spring中自定义注解与AOP中获取注解
- spring aop使用注解从切面中获取参数
- Spring自定义注解+Aop记录用户操作日志
- SpringAOP拦截Controller,Service实现日志管理(自定义注解的方式)
- Spring AOP自定义注解解决日志和签名校验
- spring-redis缓存方案学习三:基于aop的自定义注解开发
- Spring AOP自定义注解实现系统日志记录管理
- 利用Spring AOP自定义注解解决日志和签名校验
- 使用自定义注解+Spring AOP 实现日志记录
- Spring AOP+注解的方式实现缓存的获取
- SpringAOP实例自定义Annotation形式详解
- 利用Spring AOP处理自定义注解
- SpringAOP+自定义注解模拟shiro框架实现
- springAOP进行自定义注解,用于方法的处理