pom文件中
<!--引入AOPjar包文件-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
配置类:
package com.jt.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
@ComponentScan("com.jt")
@EnableAspectJAutoProxy //使用Spring机制 使用Springboot默认开启
public class SpringConfig {
}
接口和实现类
package com.jt.service;
public interface UserService {
void say();
}
package com.jt.service;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService{
@Override
public void say() {
System.out.println("我是小小秦");
}
}
测试:
package com.jt;
import com.jt.config.SpringConfig;
import com.jt.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class TestAop {
@Test
public void run(){
ApplicationContext context=new AnnotationConfigApplicationContext(SpringConfig.class);
UserService userService = context.getBean(UserService.class);
userService.say();
}
}
切面:
package com.jt.aop;
import com.jt.anno.Privilege;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@Component
@Aspect
public class SpringAop {
@Pointcut("bean(userServiceImpl)")
public void pointCut(){
}
@Around("pointCut()")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
long l = System.currentTimeMillis();
Object proceed1 = joinPoint.proceed();
long l1 = System.currentTimeMillis();
long time=l1-l;
System.out.println("方法执行的时间为:"+time);
System.out.println("目标对象类型"+joinPoint.getTarget().getClass());
System.out.println("类的名称"+joinPoint.getSignature().getDeclaringTypeName());
System.out.println("方法名称"+joinPoint.getSignature().getName());
System.out.println("参数信息"+ Arrays.toString(joinPoint.getArgs()));
return proceed1;
}
}
下一题:权限
package com.jt.aop;
import com.jt.anno.Privilege;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
@Component
@Aspect
public class SpringAop {
private List<String> list=new ArrayList<>(); //单机版本
//java基础:ArraryList是线程不安全的。用vector<>
@PostConstruct
public void init(){
list.add("add"); //add操作
list.add("update"); //update操作
}
/**
* 1. 获取方法中的注解@Pri(name = "add")
* 2. 获取权限的名称 name = "add"
* 3. 判断list集合中是否有该权限.
* true: 有权限 执行目标方法
* false: 输出没有权限直接跳过.
* 4. 规则:
* 1. 如果有多个参数 则joinPoint必须位于第一位!!!! args[0]
* 变态写法:
* 可以直接写注解的名称,这样的话 可以直接为注解赋值.
* @param joinPoint
* @return
* @throws Throwable
*/
@Around("@annotation(privilege)") //只是拦截注解,获取注解的数据
public Object around2(ProceedingJoinPoint joinPoint,Privilege privilege) throws Throwable {
String name=privilege.name();
System.out.println("用户的权限:"+name);
if (list.contains(name)){
Object proceed = joinPoint.proceed();
return proceed;
}else {
System.out.println("告知用户没有权限");
return null;
}
}
}