spring前置后置通知实现
前言
Pointcut注解表达式execution、@target、@annotation、@within等:
https://blog.csdn.net/HD243608836/article/details/100174658
Spring中@within与@target的一些区别:
https://blog.csdn.net/qq_46388795/article/details/120265883
aop结构:
https://zhuanlan.zhihu.com/p/244864171
其他参考信息:
https://www.jianshu.com/p/85a1ea6c7bdc
一下代码下载地址:
https://files-cdn.cnblogs.com/files/blogs/679445/hdxspringinit.zip
1、样例:类EventIntercept
package com.hdx.common.hdxspringinit.aop; import com.hdx.common.hdxspringinit.annotation.MethodAnnotation; import com.hdx.common.hdxspringinit.annotation.MyAnnotation; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; import java.lang.reflect.Method; // 触发优先级 @Order(-1) // 通过配置控制当前类是否生效 name=属性文件中配置的key havingValue=属性文件中配置的值相等生效 matchIfMissing=true如果没有配置默认生效 @ConditionalOnProperty(name = "spring.datasource.type", havingValue = "org.apache.tomcat.jdbc.pool.DataSource", matchIfMissing = true) @Aspect @Component public class EventIntercept { // https://www.jianshu.com/p/85a1ea6c7bdc // https://zhuanlan.zhihu.com/p/244864171 aop结构 // Spring中@within与@target的一些区别 // https://blog.csdn.net/qq_46388795/article/details/120265883 // Pointcut注解表达式execution、@target、@annotation、@within等 // https://blog.csdn.net/HD243608836/article/details/100174658 /** * * 拦截条件 * 1、类上含有 MyAnnotation注解 * 2、当前方法上有MethodAnnotation 注解 * * @param joinPoint * @param myAnnotation * @param methodAnnotation * @throws Exception */ //@execution(com.hdx.common.hdxspringinit.evnettest..*) // && execution(* com.hdx.common.hdxspringinit.evnettest.*.*(..)) // && @within(com.hdx.common.hdxspringinit.annotation.MyAnnotation) // && @annotation(com.hdx.common.hdxspringinit.annotation.MethodAnnotation) @Before("@within(myAnnotation) && @annotation(methodAnnotation)") public void afterPropertiesSet2(JoinPoint joinPoint, MyAnnotation myAnnotation, MethodAnnotation methodAnnotation) throws Exception { //获取当前拦截的原始类对象 joinPoint.getTarget(); //获取当前拦截的原始类对象上面的注解 joinPoint.getTarget().getClass().getAnnotation(MyAnnotation.class); // 获取当前拦截的方法对象 Method targetMethod =((MethodSignature)joinPoint.getSignature()).getMethod(); // 根据反射获取被代理原始的方法对象参数 Method realMethod = joinPoint.getTarget().getClass().getDeclaredMethod(joinPoint.getSignature().getName(), targetMethod.getParameterTypes()); // 获取当前方法的入参值 Object[] vals = joinPoint.getArgs(); // 获取当前拦截方法的变量名称 ((MethodSignature) joinPoint.getSignature()).getParameterNames(); System.out.printf(""); } @After("execution(* com.hdx.common.hdxspringinit.evnettest.*.*(..))") public void afterPropertiesSet2() throws Exception { System.out.printf(""); } }
2、自定义注解:MethodAnnotation 、MyAnnotation
MyAnnotation
package com.hdx.common.hdxspringinit.annotation; import java.lang.annotation.*; @Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Inherited @Documented public @interface MyAnnotation { String name(); }
MethodAnnotation
package com.hdx.common.hdxspringinit.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface MethodAnnotation { String name(); }
3、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.7.0</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.hdx.common</groupId> <artifactId>hdxspringinit</artifactId> <version>0.0.1-SNAPSHOT</version> <name>hdxspringinit</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <repackage.classifier/> <spring-native.version>0.12.0-SNAPSHOT</spring-native.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>5.3.3</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>2.4.2</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.6</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> <classifier>${repackage.classifier}</classifier> <image> <builder>paketobuildpacks/builder:tiny</builder> <env> <BP_NATIVE_IMAGE>true</BP_NATIVE_IMAGE> </env> </image> </configuration> </plugin> <plugin> <groupId>org.springframework.experimental</groupId> <artifactId>spring-aot-maven-plugin</artifactId> <version>${spring-native.version}</version> <executions> <execution> <id>test-generate</id> <goals> <goal>test-generate</goal> </goals> </execution> <execution> <id>generate</id> <goals> <goal>generate</goal> </goals> </execution> </executions> </plugin> </plugins> </build> <repositories> <repository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/snapshot</url> <releases> <enabled>false</enabled> </releases> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/snapshot</url> </pluginRepository> </pluginRepositories> <profiles> <profile> <id>native</id> <properties> <repackage.classifier>exec</repackage.classifier> <native-buildtools.version>0.9.11</native-buildtools.version> </properties> <dependencies> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-launcher</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.graalvm.buildtools</groupId> <artifactId>native-maven-plugin</artifactId> <version>${native-buildtools.version}</version> <extensions>true</extensions> <executions> <execution> <id>test-native</id> <phase>test</phase> <goals> <goal>test</goal> </goals> </execution> <execution> <id>build-native</id> <phase>package</phase> <goals> <goal>build</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </profile> </profiles> </project>

浙公网安备 33010602011771号