springAOP进行自定义注解,用于方法的处理
2017-08-31 15:50
696 查看
本文采用的spring boot进行配置
maven 引入
<!-- spring boot aop starter依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
application.properties文件开启aop注解
spring.aop.auto = true;
自定义注解类
切点类
service或controller层调用
@SystemServiceType(type = 1,description = "根据pcode获取下级列表",classType = SysDictionaryInfoVO.class)
public Object getChildDicVosByPcode(String pcode) throws Exception{
List<SysDictionaryInfo> dictionaryInfos = dictionaryInfoMapper.selectChildDictionaryByPcode(pcode);
List<SysDictionaryInfoVO> sysDictionaryInfoVOs = new ArrayList<SysDictionaryInfoVO>();
return dictionaryInfos;
}
这里将会对返回结果dictionaryInfos为SysDictionaryInfo集合,在Around环绕通知进行结果的转换,返回的结果为SysDictionaryInfoVO,
由于转换前和转换后的类型不一样,所有需要定义方法的返回类型为Object
此外,可以在前置通知、异常通知等通知中进行日志的处理
参考:http://blog.csdn.net/czmchen/article/details/42392985
http://blog.csdn.net/liuchuanhong1/article/details/55099753
maven 引入
<!-- spring boot aop starter依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
application.properties文件开启aop注解
spring.aop.auto = true;
自定义注解类
package com.kuaixin.crm.crm_tsale_kx_service.service.anno; import java.lang.annotation.*; /** *自定义注解 拦截service */ @Target({ElementType.PARAMETER, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface SystemServiceType { /** * 方法描述 * @return */ String description() default ""; /** * 方法类型 0 表示不进行处理,1 表示进行处理 * @return */ int type() default 0; /** * 类的元数据,用于指定需要转换为的目标格式 * @return */ Class classType(); }
切点类
package com.kuaixin.crm.crm_tsale_kx_service.service.anno; import org.apache.commons.beanutils.BeanUtils; import org.apache.log4j.LogManager; import org.apache.log4j.Logger; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import java.lang.annotation.Annotation; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; /** * Created by Administrator on 2017/8/31. * @desc 定义切点类,无论是前置通知还是后置通知、环绕通知以及异常通知,都需要在指定的方法上加上SystemServiceType注释就会生效 * 还可以在通知中记录日志 */ @Component // 注册到Spring容器,必须加入这个注解 @Aspect // 该注解标示该类为切面类,切面是由通知和切点组成的。 public class SystemServiceTypeAspect { //注入Service用于把日志保存数据库 /* @Resource private LogService logService;*/ //日志记录对象 private final static Logger log = LogManager.getLogger(SystemServiceTypeAspect.class); //Service层切点 @Pointcut("@annotation(com.kuaixin.crm.crm_tsale_kx_service.service.anno.SystemServiceType)") public void serviceAspect() { } //controller层切点 com.kuaixin.crm.crm_tsale_kx_service.service.anno.SystemServiceType可以指定另外定义的注释接口 @Pointcut("@annotation(com.kuaixin.crm.crm_tsale_kx_service.service.anno.SystemServiceType)") public void controllerAspect() { } /** *对某个方法返回的结果进行处理后,如将entity转换为与前端交互的vo */ @Around(value = "serviceAspect()") public Object aroundProcess(ProceedingJoinPoint pjp) throws Throwable { Object retVal = pjp.proceed(); //*==========记录本地异常日志==========*//* //logger.error("异常方法:{}异常代码:{}异常信息:{}参数:{}", joinPoint.getTarget().getClass().getName() + joinPoint.getSignature().getName(), e.getClass().getName(), e.getMessage(), params); //需要转换为的vo对象Class Class vClass = getClassByAnno(pjp); //数组或集合对象 if(retVal.getClass().isArray()||retVal instanceof List){ List
service或controller层调用
@SystemServiceType(type = 1,description = "根据pcode获取下级列表",classType = SysDictionaryInfoVO.class)
public Object getChildDicVosByPcode(String pcode) throws Exception{
List<SysDictionaryInfo> dictionaryInfos = dictionaryInfoMapper.selectChildDictionaryByPcode(pcode);
List<SysDictionaryInfoVO> sysDictionaryInfoVOs = new ArrayList<SysDictionaryInfoVO>();
return dictionaryInfos;
}
这里将会对返回结果dictionaryInfos为SysDictionaryInfo集合,在Around环绕通知进行结果的转换,返回的结果为SysDictionaryInfoVO,
由于转换前和转换后的类型不一样,所有需要定义方法的返回类型为Object
此外,可以在前置通知、异常通知等通知中进行日志的处理
参考:http://blog.csdn.net/czmchen/article/details/42392985
http://blog.csdn.net/liuchuanhong1/article/details/55099753
相关文章推荐
- 自定义注解实现spring 方法拦截用于日志,等处理
- 自定义注解及利用AOP对方法进行加强处理
- 利用Spring AOP处理自定义注解
- java基于spring注解AOP的异常处理的方法
- SpringAOP使用入门,如何对自定义的注解进行切面编程
- 使用方法拦截器MethodInterceptor和AOP统一处理log (Spring的拦截器 对方法进行拦截 ))
- 使用Spring注解AOP(基于自定义注解和包下拦截方法)
- 通过spring的aop注解实现所有方法异常的捕获处理,sqlite SQLITE_BUSY异常
- 使用Spring Boot的AOP处理自定义注解
- 详解使用Spring Boot的AOP处理自定义注解
- 详解使用Spring AOP和自定义注解进行参数检查
- 利用Spring AOP处理自定义注解
- 自己试验在spring的环绕通知里获取目标对象的类名和目标方法的参数类名,用于根据自定义注解判断访问权限,有没有更好的办法,高手指点一下
- Spring Aop自定义注解拦截Controller实现日志管理
- java SpringAOP拦截Controller,Service实现日志管理(自定义注解的方式)
- 从头认识Spring-3.6 简单的AOP日志实现(注解版)-需要记录方法的运行时间
- spring-AOP通过注解@DeclareParents引入新的方法
- 在spring的aop中获取自定义注解的参数值,即在切面中获取annotation的参数值
- Spring boot异常统一处理方法:@ControllerAdvice注解的使用、全局异常捕获、自定义异常捕获
- 如何用Spring AOP和Java自定义注解实现登陆鉴权