普通方式:
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