Skip to content

Commit

Permalink
aop模块、scheduler模块
Browse files Browse the repository at this point in the history
  • Loading branch information
guolanren committed Apr 11, 2020
1 parent 7cb2be1 commit b53d5bb
Show file tree
Hide file tree
Showing 32 changed files with 498 additions and 356 deletions.
5 changes: 5 additions & 0 deletions aop/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Spring Boot Aop

## 概述

Spring Boot Aop 概述...
21 changes: 1 addition & 20 deletions api-auditing/pom.xml → aop/aop-annotation/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,11 @@
</parent>

<groupId>name.guolanren</groupId>
<artifactId>api-auditing</artifactId>
<artifactId>aop-annotation</artifactId>
<version>1.0.0</version>
<name>api-auditing</name>
<description>API 审计</description>

<properties>
<java.version>1.8</java.version>
<fastjson.version>1.2.58</fastjson.version>
</properties>

<dependencies>
Expand All @@ -38,22 +35,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-core</artifactId>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>

<build>
Expand Down
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);
}

}
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 "";
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ApiAuditing {
String menu() default "";
String type() default "";
}
public @interface AopAnnotations {

AopAnnotation[] value();

}
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;
}
}

}
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;
}
}
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.
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");
}
}
48 changes: 48 additions & 0 deletions aop/aop-common/pom.xml
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>
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);
}

}
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...");
}
}

}
Loading

0 comments on commit b53d5bb

Please sign in to comment.