自定义注解及利用AOP对方法进行加强处理
2017-07-21 21:20
477 查看
自定义注解及利用AOP对方法进行加强处理
自定义注解
自定义的关键字为@Interface原注解
原注解是注解的注解,有一下几种@Retention: 定义注解的保留策略
@Retention(RetentionPolicy.SOURCE) //注解仅存在于源码中,在class字节码文件中不包含
@Retention(RetentionPolicy.CLASS) // 默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得,
@Retention(RetentionPolicy.RUNTIME) // 注解会在class字节码文件中存在,在运行时可以通过反射获取到
@Target
@Target:注解的目标,即注解的范围,可以为类或者方法或者同时在类和方法等
@Documented
说明该注解将被包含在javadoc中
例子如下
/** * * @author 734621 * */ @Target({ElementType.METHOD})//注解定义在方法还是类中,是一个数组,即可以同时定义在类和方法中 @Retention(RetentionPolicy.RUNTIME)// 注解会在class字节码文件中存在,在运行时可以通过反射获取到 public @interface CommonLogging { LogType type() default LogType.ALL; }
利用AOP及注解对方法进行加强
即通过Spring的AOP及注解的范围对方法进行加强,加强的意思是在方法执行前或后加一些东西并且可以改变方法的行为。例子如下package com.sf.rps.spring.apect; import java.lang.reflect.Method; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.core.Ordered; import org.springframework.stereotype.Component; /** * 切面类,定义注解需要做的操作 * @author 734621 * */ @Component//加入Spring管理 @Aspect//标注此类是切面类 public class CommonLoggingAdvice implements Ordered{//Ordered是如果有多个注解的话会先执行哪个注解,后执行哪个注解,order的值越小就越先执行,执行完方法后则相反 /** * 定义切入点,切入范围,即定义了注解CommonLogging才会执行 */ @Pointcut(value = "@annotation(com.sf.rps.spring.apect.CommonLogging)") private void pointcut(){ } /** * 注解中的pointcut()和logging对应方法的参数值,缺一不可 * @param joinPoint * @param logging * @return */ @Around(value = "pointcut() && @annotation(logging)") public Object otherAround(ProceedingJoinPoint joinPoint,CommonLogging logging){ MethodSignature signature = (MethodSignature)joinPoint.getSignature(); Method method = signature.getMethod();//获得被注解的方法 Object [] args = joinPoint.getArgs();//获得被注解的参数 Object result = null; System.out.println("2222222222"); if (LogType.BEFORE == logging.type()) {//判断注解里的值 before(method.getName()); result = executeMethod(joinPoint);//执行方法 }else if(LogType.AFTER == logging.type()){ result = executeMethod(joinPoint); after(method.getName()); }else{ before(method.getName()); result = executeMethod(joinPoint); after(method.getName()); } return result; } /** * 执行完被注解的方法后需执行的方法 * pointcut() && logging 与方法参数必须保持一致 * @param joinPoint * @param logging * @param result被注解方法的返回值 * @return */ @AfterReturning(value = "pointcut() && @annotation(logging)",returning = "result") public Object afterRunning(JoinPoint joinPoint,CommonLogging logging,Object result){ System.out.println("-------afterRunning---------"); return result; } public Object executeMethod(ProceedingJoinPoint joinPoint){ try { return joinPoint.proceed();//执行被注解的方法,并且返回返回值 } catch (Throwable e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } public void before(String methodName){ System.out.println("方法"+methodName+"将开始执行!"); } public void after(String methodName){ System.out.println("方法"+methodName+"已经执行完毕!"); } @Override public int getOrder() { // TODO Auto-generated method stub return 10; } }
import org.springframework.stereotype.Component; @Component("loggerServer") public class LoggerServer { @CommonLogging(type = LogType.ALL) public String getLoggerMessage(){ System.out.println("this is method getLoggerMessage"); return "Simba"; } }
import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(value=SpringJUnit4ClassRunner.class) @ContextConfiguration(value = {"classpath:beanFactory.xml"}) public class TestLogType { @Resource(name = "loggerServer") private LoggerServer loggerServer; @Test public void testLogType(){ System.out.println(loggerServer.getLoggerMessage()); } }定义配置文件beanFactory.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 开启注解 --> <context:annotation-config/> <!-- 开启切面 --> <aop:aspectj-autoproxy proxy-target-class="true" /> <!-- 定义那个包下可以注解 --> <context:component-scan base-package="com.sf.rps.spring.apect"></context:component-scan> </beans>
最后执行结果如下:
2222222222
方法getLoggerMessage将开始执行!//方法执行前的操作
this is method getLoggerMessage
方法getLoggerMessage已经执行完毕!//方法执行后的操作
-------afterRunning---------//方法执行后的操作
Simba//返回值
相关文章推荐
- springAOP进行自定义注解,用于方法的处理
- 利用AOP和自定义注解进行Token校验
- 利用Spring AOP处理自定义注解
- Android界面之----自定义的Dialog,然后利用回调方法,在调用处,进行Dialog中各按钮的事件处理。
- 利用Spring AOP处理自定义注解
- spring源码剖析(五)利用AOP实现自定义Spring注解
- 利用Aop的环绕通知进行权限的处理
- 利用Spring AOP (aspect) 自定义注解解决日志和签名校验
- SpringMVC利用AOP实现自定义注解记录日志
- 利用spring AOP 和注解实现方法中查cache-我们到底能走多远系列(46)
- 浅谈利用numpy对矩阵进行归一化处理的方法
- SpringMVC利用AOP实现自定义注解记录日志
- Java注解教程:自定义注解示例,利用反射进行解析
- 使用Spring注解AOP(基于自定义注解和包下拦截方法)
- Spring boot异常统一处理方法:@ControllerAdvice注解的使用、全局异常捕获、自定义异常捕获
- C#中利用泛型扩展方法辅助枚举类型进行Flags处理
- 如何自定义注解Annotation,并利用反射进行解析
- SpringMVC利用AOP实现自定义注解记录日志
- 使用Spring Boot的AOP处理自定义注解
- 利用AOP获取自定义标签的参数以及方法的参数