一、不结合注解方式的aop
1、首先要见一个springboot的项目
2、pom.xml中引入包
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
3、配置application.properties
4、编写Aspect类
package owner; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.stereotype.Component; @Aspect @Component public class LogOwnerAspect { @Pointcut("execution(public * owner.*.*(..))") public void execute(){} @Before("execute()") public void before(){ System.out.println("LogOwnerAspect方法执行前"); } @Around("execute()") public Object around(ProceedingJoinPoint pjd) throws Throwable{ System.out.println("LogOwnerAspect环绕执行"); Object res= pjd.proceed(); System.out.println("LogOwnerAspect环绕执行hou"+res); return res; } @After("execute()") public void after(){ System.out.println("LogOwnerAspect方法执行后"); } /* @Around("execute()") public void around(ProceedingJoinPoint pjd) throws Throwable{ System.out.println("环绕执行"); Object res= pjd.proceed(); System.out.println("环绕执行2"+res); }*/ }
5、编写后面的类
package owner; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/Owner") public class UserOwnerController { @RequestMapping("/first") public String first(){ return "first Controller"; } @RequestMapping("/second") @UserAccessOwner() public String second(){ return "second Controller"; } }
6、编写启动类
package owner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class OwnerApplication { public static void main(String[] args) { SpringApplication.run(OwnerApplication.class); } }
7、运行OwnerApplication,访问http://localhost:8092/Owner/first
二、结合注解aop
1、首先要见一个springboot的项目
2、pom.xml中引入包
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
3、配置application.properties
4、编写注解
package owner; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface UserAccessOwner { String value() default "无信息"; }
5、编写Aspect类
package owner; 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.aspectj.lang.reflect.MethodSignature; import org.springframework.stereotype.Component; import java.lang.reflect.Method; @Aspect @Component public class UserAccessOwnerAspect { @Pointcut("@annotation(owner.UserAccessOwner)") public void access(){} @Around("access()") public Object around(ProceedingJoinPoint joinPoint) throws Throwable{ System.out.println("执行前"); Method method =((MethodSignature)joinPoint.getSignature()).getMethod(); /*System.out.println(method.getName()); System.out.println(method.getAnnotation(UserAccessOwner.class));*/ UserAccessOwner userAccessOwner= method.getAnnotation(UserAccessOwner.class); System.out.println("second around:"+userAccessOwner.value()); System.out.println(method.getDeclaringClass()); Object object=joinPoint.proceed(); System.out.println("执行后"+object); return object; } } 6、编写后面的类
package owner; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/Owner") public class UserOwnerController { @RequestMapping("/first") public String first(){ return "first Controller"; } @RequestMapping("/second") @UserAccessOwner("自己的") public String second(){ return "second Controller"; } }
7、编写启动类
package owner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class OwnerApplication { public static void main(String[] args) { SpringApplication.run(OwnerApplication.class); } }
8、运行OwnerApplication,访问http://localhost:8092/Owner/second