-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
498 additions
and
356 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Spring Boot Aop | ||
|
||
## 概述 | ||
|
||
Spring Boot Aop 概述... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
aop/aop-annotation/src/main/java/name/guolanren/AopAnnotationApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package name.guolanren; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
/** | ||
* @author guolanren | ||
*/ | ||
@SpringBootApplication | ||
public class AopAnnotationApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(AopAnnotationApplication.class, args); | ||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
aop/aop-annotation/src/main/java/name/guolanren/annotation/AopAnnotation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package name.guolanren.annotation; | ||
|
||
import org.springframework.core.annotation.AliasFor; | ||
|
||
import java.lang.annotation.*; | ||
|
||
/** | ||
* @author guolanren | ||
*/ | ||
@Target({ElementType.METHOD}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
@Repeatable(AopAnnotations.class) | ||
public @interface AopAnnotation { | ||
|
||
@AliasFor("condition") | ||
String value() default "true"; | ||
|
||
@AliasFor("value") | ||
String condition() default ""; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
aop/aop-annotation/src/main/java/name/guolanren/aspect/AopAnnotationAspect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package name.guolanren.aspect; | ||
|
||
import name.guolanren.annotation.AopAnnotation; | ||
import name.guolanren.annotation.AopAnnotations; | ||
import org.aspectj.lang.ProceedingJoinPoint; | ||
import org.aspectj.lang.annotation.*; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* @author guolanren | ||
*/ | ||
@Aspect | ||
@Component | ||
public class AopAnnotationAspect extends SpelSupportedAspect { | ||
|
||
|
||
@Around("@annotation(aopAnnotations)") | ||
public Object aroundMatch(ProceedingJoinPoint joinPoint, AopAnnotations aopAnnotations) throws Throwable { | ||
try { | ||
addVariable(joinPoint); | ||
boolean requiredAop = true; | ||
for (AopAnnotation aopAnnotation : aopAnnotations.value()) { | ||
requiredAop = requiredAop && getValue(aopAnnotation.condition(), Boolean.class); | ||
} | ||
if (requiredAop) { | ||
System.out.println("Around: before required aop..."); | ||
} | ||
Object result = joinPoint.proceed(); | ||
return result; | ||
} catch (Throwable e) { | ||
throw e; | ||
} | ||
} | ||
|
||
} |
54 changes: 54 additions & 0 deletions
54
aop/aop-annotation/src/main/java/name/guolanren/aspect/SpelSupportedAspect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package name.guolanren.aspect; | ||
|
||
import org.aspectj.lang.ProceedingJoinPoint; | ||
import org.aspectj.lang.reflect.MethodSignature; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.BeanFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.expression.BeanFactoryResolver; | ||
import org.springframework.core.LocalVariableTableParameterNameDiscoverer; | ||
import org.springframework.expression.ExpressionParser; | ||
import org.springframework.expression.spel.standard.SpelExpressionParser; | ||
import org.springframework.expression.spel.support.StandardEvaluationContext; | ||
import org.springframework.lang.Nullable; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
/** | ||
* @author guolanren | ||
*/ | ||
public abstract class SpelSupportedAspect { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(SpelSupportedAspect.class); | ||
|
||
@Autowired | ||
private BeanFactory beanFactory; | ||
|
||
StandardEvaluationContext context = new StandardEvaluationContext(); | ||
ExpressionParser parser = new SpelExpressionParser(); | ||
LocalVariableTableParameterNameDiscoverer discoverer = new LocalVariableTableParameterNameDiscoverer(); | ||
|
||
public void addVariable(ProceedingJoinPoint joinPoint) { | ||
try { | ||
context.setBeanResolver(new BeanFactoryResolver(beanFactory)); | ||
Method method = ((MethodSignature)joinPoint.getSignature()).getMethod(); | ||
String[] params = discoverer.getParameterNames(method); | ||
Object[] args = joinPoint.getArgs(); | ||
for (int i = 0; i < params.length; i++) { | ||
context.setVariable(params[i], args[i]); | ||
} | ||
} catch (Exception e) { | ||
LOG.warn(e.getMessage()); | ||
} | ||
} | ||
|
||
public <T> T getValue(String expressionString, @Nullable Class<T> desiredResultType) { | ||
try { | ||
return parser.parseExpression(expressionString).getValue(context, desiredResultType); | ||
} catch (Exception e) { | ||
LOG.warn(e.getMessage()); | ||
} | ||
return null; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
aop/aop-annotation/src/main/java/name/guolanren/service/AopAnnotationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package name.guolanren.service; | ||
|
||
import name.guolanren.annotation.AopAnnotation; | ||
import name.guolanren.annotation.AopAnnotations; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.util.StringUtils; | ||
|
||
/** | ||
* @author guolanren | ||
*/ | ||
@Service | ||
public class AopAnnotationService { | ||
|
||
@AopAnnotations({ | ||
@AopAnnotation(condition = "true'") | ||
}) | ||
public String requiredAopService(String name) { | ||
if (StringUtils.isEmpty(name)) { | ||
throw new RuntimeException("名字不能为空"); | ||
} | ||
System.out.printf("service for %s ...%n", name); | ||
return name.toUpperCase(); | ||
} | ||
|
||
@AopAnnotations({ | ||
@AopAnnotation(condition = "false") | ||
}) | ||
public String noNeedAopService(String name) { | ||
if (StringUtils.isEmpty(name)) { | ||
throw new RuntimeException("名字不能为空"); | ||
} | ||
System.out.printf("service for %s ...%n", name); | ||
return name.toUpperCase(); | ||
} | ||
|
||
@AopAnnotations({ | ||
@AopAnnotation(condition = "#name == 'guolanren'") | ||
}) | ||
public String service(String name) { | ||
if (StringUtils.isEmpty(name)) { | ||
throw new RuntimeException("名字不能为空"); | ||
} | ||
System.out.printf("service for %s ...%n", name); | ||
return name.toUpperCase(); | ||
} | ||
} |
Empty file.
30 changes: 30 additions & 0 deletions
30
aop/aop-annotation/src/test/java/name/guolanren/service/AopAnnotationServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package name.guolanren.service; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
@SpringBootTest | ||
@RunWith(SpringRunner.class) | ||
public class AopAnnotationServiceTest { | ||
|
||
@Autowired | ||
private AopAnnotationService aopAnnotationService; | ||
|
||
@Test | ||
public void testRequiredAopService() { | ||
aopAnnotationService.requiredAopService("guolanren"); | ||
} | ||
|
||
@Test | ||
public void testNoNeedAopService() { | ||
aopAnnotationService.noNeedAopService("guolanren"); | ||
} | ||
|
||
@Test | ||
public void testService() { | ||
aopAnnotationService.service("guolanren"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?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 http://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.1.6.RELEASE</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
|
||
<groupId>name.guolanren</groupId> | ||
<artifactId>aop-common</artifactId> | ||
<version>1.0.0</version> | ||
|
||
<properties> | ||
<java.version>1.8</java.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</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> |
16 changes: 16 additions & 0 deletions
16
aop/aop-common/src/main/java/name/guolanren/AopCommonApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package name.guolanren; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
/** | ||
* @author guolanren | ||
*/ | ||
@SpringBootApplication | ||
public class AopCommonApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(AopCommonApplication.class, args); | ||
} | ||
|
||
} |
59 changes: 59 additions & 0 deletions
59
aop/aop-common/src/main/java/name/guolanren/aspect/AopCommonAspect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package name.guolanren.aspect; | ||
|
||
import org.aspectj.lang.ProceedingJoinPoint; | ||
import org.aspectj.lang.annotation.*; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* @author guolanren | ||
*/ | ||
@Aspect | ||
@Component | ||
public class AopCommonAspect { | ||
|
||
@Pointcut("execution(* name.guolanren..*(..))") | ||
public void allMatch() {} | ||
|
||
@Pointcut("execution(public * name.guolanren..*(..))") | ||
public void allPublicMatch() {} | ||
|
||
@Before("allMatch()") | ||
public void beforeAllMatch() { | ||
System.out.println("Before allMatch..."); | ||
} | ||
|
||
@Before("allPublicMatch()") | ||
public void beforeAllPublicMatch() { | ||
System.out.println("Before: before allPublicMatch..."); | ||
} | ||
|
||
@After("allMatch()") | ||
public void afterAllMatch() { | ||
System.out.println("After: after allMatch..."); | ||
} | ||
|
||
@AfterReturning(pointcut = "allMatch()", returning = "retVal") | ||
public void afterReturningMatch(Object retVal) { | ||
System.out.printf("AfterReturning: after returning allMatch, retVal is %s...%n", retVal); | ||
} | ||
|
||
@AfterThrowing(pointcut = "allMatch()", throwing = "e") | ||
public void afterThrowingMatch(Throwable e) { | ||
System.out.printf("AfterThrowing: after throwing allMatch, exception is %s...%n", e.getMessage()); | ||
} | ||
|
||
@Around("allMatch()") | ||
public Object aroundMatch(ProceedingJoinPoint joinPoint) throws Throwable { | ||
try { | ||
System.out.println("Around: before allMatch..."); | ||
Object result = joinPoint.proceed(); | ||
return result; | ||
} catch (Throwable e) { | ||
System.out.printf("Around: throwing allMatch, exception is %s...%n", e.getMessage()); | ||
throw e; | ||
} finally { | ||
System.out.println("Around: after allMatch..."); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.