spring boot之aop常用的两种拦截方式(3)

普通方式:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值