spring boot之aop常用的两种拦截方式(3)
2018-01-28 15:10
429 查看
普通方式:
注解方式:
aop配置:
拦截规则设置:execution(* com.zh.ch1.aop.*.*(..))
方法规则式拦截,addUser
15:00:35.468 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'demoMethodService'
.....task...
方法规则式拦截,add
package com.zh.ch1.aop; import org.springframework.stereotype.Service; @Service public class DemoMethodService { public void add(){ System.out.println(".....task..."); } }
注解方式:
package com.zh.ch1.aop; 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; /** * 自定义java 注解 * METHOD:作用于方法上 * 该注解应用范围:RUNTIME * @author Hiiso * */ @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Action { String name(); }
package com.zh.ch1.aop; import org.springframework.stereotype.Service; @Service public class DemoAnnotationService { /** * 使用@Action注解 */ @Action(name = "addUser!") public void addUser() { } }
aop配置:
package com.zh.ch1.aop; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.EnableAspectJAutoProxy; @Configuration @ComponentScan("com.zh.ch1.aop") @EnableAspectJAutoProxy //1 public class AopConfig { }
package com.zh.ch1.aop; import java.lang.reflect.Method; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.stereotype.Component; @Aspect // 1:通过改注解声明一个切面 @Component // 2让切面称为spring管理的bean public class LogAspect { @Pointcut("@annotation(com.zh.ch1.aop.Action)") // 3:该注解声明切入点 public void annotationPointCut() { }; //注解 /*@After("annotationPointCut()") // 4:使用刚声明的切入点(annotationPointCut()) public void after(JoinPoint joinPoint) { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); Action action = method.getAnnotation(Action.class); System.out.println("注解式拦截 " + action.name()); // 5:通过反射获取注解的属性值 }*/ //切面 @After("execution(* com.zh.ch1.aop.*.*(..))") //6:直接拦截方法名 public void before(JoinPoint joinPoint){ MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); System.out.println("方法规则式拦截,"+method.getName()); } }
package com.zh.ch1.aop; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Main { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AopConfig.class); // 1 //注解 DemoAnnotationService demoAnnotationService = context.getBean(DemoAnnotationService.class); demoAnnotationService.addUser(); //切面 DemoMethodService demoMethodService = context.getBean(DemoMethodService.class); demoMethodService.add(); context.close(); } }
拦截规则设置:execution(* com.zh.ch1.aop.*.*(..))
方法规则式拦截,addUser
15:00:35.468 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'demoMethodService'
.....task...
方法规则式拦截,add
相关文章推荐
- Spring-boot中读取config配置文件的两种方式
- SpringAOP拦截Controller,Service实现日志管理(自定义注解的方式)
- SpringAOP拦截Controller,Service实现日志管理(自定义注解的方式)
- Spring Aop的两种方式
- Spring Boot干货系列:(十)开发常用的热部署方式汇总
- Spring中AOP的两种代理方式(Java动态代理和CGLIB代理)
- Spring中的两种AOP织入方式
- springBoot使用Controller读取配置文件两种方式&读取自定义配置方法
- Spring中AOP的两种代理方式(Java动态代理和CGLIB代理)
- Spring中AOP的两种代理方式(Java动态代理和CGLIB代理)
- Spring Boot 注解启动的两种方式
- 【框架】[Spring]纯Java的方式实现AOP切面(拦截)技术
- 搭建与测试Spring的开发环境_容器常用的两种方式
- Spring中AOP的两种代理方式(Java动态代理和CGLIB代理)
- JavaWeb日记——Springboot 热部署的两种方式
- SpringAOP拦截Controller,Service实现日志管理(自定义注解的方式)
- Spring学习_03_AOP在Spring中的两种实现方式
- spring boot集成pagehelper,两种方式
- spring 中常用的两种事务配置方式以及事务的传播性、隔离级别
- Spring中AOP的两种代理方式(Java动态代理和CGLIB代理)