diff --git a/spring-boot/spring-boot-common/pom.xml b/spring-boot/spring-boot-common/pom.xml new file mode 100644 index 0000000..caf3cc2 --- /dev/null +++ b/spring-boot/spring-boot-common/pom.xml @@ -0,0 +1,74 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.6.7 + + + com.wyl + spring-boot-common + 0.0.1-SNAPSHOT + spring-boot-common + Demo project for Spring Boot + + 1.8 + + + + org.springframework.boot + spring-boot-starter + + + + + + + org.springframework.boot + spring-boot-starter-aop + + + org.aspectj + aspectjweaver + + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework + spring-tx + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.projectlombok + lombok + + + + + + + + diff --git a/spring-boot/spring-boot-common/src/main/java/com/wyl/spring/boot/common/SpringBootCommonApplication.java b/spring-boot/spring-boot-common/src/main/java/com/wyl/spring/boot/common/SpringBootCommonApplication.java new file mode 100644 index 0000000..3106988 --- /dev/null +++ b/spring-boot/spring-boot-common/src/main/java/com/wyl/spring/boot/common/SpringBootCommonApplication.java @@ -0,0 +1,22 @@ +package com.wyl.spring.boot.common; + +import com.wyl.spring.boot.common.bean.init.BeanInitExample; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.EnableAspectJAutoProxy; +import org.springframework.scheduling.annotation.EnableAsync; + +@SpringBootApplication +@EnableAspectJAutoProxy(exposeProxy = true) +public class SpringBootCommonApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringBootCommonApplication.class, args); + } + + @Bean + BeanInitExample beanInitExample() { + return new BeanInitExample(); + } +} diff --git a/spring-boot/spring-boot-common/src/main/java/com/wyl/spring/boot/common/aop/advisor/LogAdvisor.java b/spring-boot/spring-boot-common/src/main/java/com/wyl/spring/boot/common/aop/advisor/LogAdvisor.java new file mode 100644 index 0000000..23894c8 --- /dev/null +++ b/spring-boot/spring-boot-common/src/main/java/com/wyl/spring/boot/common/aop/advisor/LogAdvisor.java @@ -0,0 +1,18 @@ +package com.wyl.spring.boot.common.aop.advisor; + +import org.springframework.aop.Pointcut; +import org.springframework.aop.support.AbstractBeanFactoryPointcutAdvisor; + +/** + * DATE 4:45 PM + * + * @author mzt. + */ +public class LogAdvisor extends AbstractBeanFactoryPointcutAdvisor { + LogPointcut logPointcut = new LogPointcut(); + + @Override + public Pointcut getPointcut() { + return logPointcut; + } +} \ No newline at end of file diff --git a/spring-boot/spring-boot-common/src/main/java/com/wyl/spring/boot/common/aop/advisor/LogAutoConfigure.java b/spring-boot/spring-boot-common/src/main/java/com/wyl/spring/boot/common/aop/advisor/LogAutoConfigure.java new file mode 100644 index 0000000..d929290 --- /dev/null +++ b/spring-boot/spring-boot-common/src/main/java/com/wyl/spring/boot/common/aop/advisor/LogAutoConfigure.java @@ -0,0 +1,25 @@ +package com.wyl.spring.boot.common.aop.advisor; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class LogAutoConfigure { + @Bean + LogPointcut logPointcut() { + return new LogPointcut(); + } + + @Bean + LogAdvisor logAdvisor() { + LogAdvisor logAdvisor = new LogAdvisor(); + logAdvisor.setAdvice(logInterceptor()); + return logAdvisor; + } + + @Bean + LogInterceptor logInterceptor() { + return new LogInterceptor(); + } +} + diff --git a/spring-boot/spring-boot-common/src/main/java/com/wyl/spring/boot/common/aop/advisor/LogInterceptor.java b/spring-boot/spring-boot-common/src/main/java/com/wyl/spring/boot/common/aop/advisor/LogInterceptor.java new file mode 100644 index 0000000..d8ad2b8 --- /dev/null +++ b/spring-boot/spring-boot-common/src/main/java/com/wyl/spring/boot/common/aop/advisor/LogInterceptor.java @@ -0,0 +1,23 @@ +package com.wyl.spring.boot.common.aop.advisor; + +import com.wyl.spring.boot.common.spel.service.Evaluator; +import org.aopalliance.intercept.MethodInterceptor; +import org.aopalliance.intercept.MethodInvocation; + +/** + * @author: wangyl + * @date: 2022/5/1 + * @description: 再方法执行前以及执行进行代理操作 + */ +public class LogInterceptor implements MethodInterceptor { + + + private final Evaluator evaluator = new Evaluator(); + @Override + public Object invoke(MethodInvocation invocation) throws Throwable { + System.out.println("Advisor 方法执行前"); + Object proceed = invocation.proceed(); + System.out.println("Advisor 方法执行后"); + return proceed; + } +} diff --git a/spring-boot/spring-boot-common/src/main/java/com/wyl/spring/boot/common/aop/advisor/LogPointcut.java b/spring-boot/spring-boot-common/src/main/java/com/wyl/spring/boot/common/aop/advisor/LogPointcut.java new file mode 100644 index 0000000..e1357b7 --- /dev/null +++ b/spring-boot/spring-boot-common/src/main/java/com/wyl/spring/boot/common/aop/advisor/LogPointcut.java @@ -0,0 +1,21 @@ +package com.wyl.spring.boot.common.aop.advisor; + +import com.wyl.spring.boot.common.aop.annotation.LogTest; +import org.springframework.aop.support.StaticMethodMatcherPointcut; + +import java.io.Serializable; +import java.lang.reflect.Method; +import java.util.Objects; + +/** + * @author: wangyl + * @date: 2022/4/30 + * @description: apo静态切入点,切入所有matches返回true的方法 + */ +public class LogPointcut extends StaticMethodMatcherPointcut implements Serializable { + @Override + public boolean matches(Method method, Class targetClass) { + LogTest annotation = method.getAnnotation(LogTest.class); + return !Objects.isNull(annotation); + } +} diff --git a/spring-boot/spring-boot-common/src/main/java/com/wyl/spring/boot/common/aop/annotation/LogTest.java b/spring-boot/spring-boot-common/src/main/java/com/wyl/spring/boot/common/aop/annotation/LogTest.java new file mode 100644 index 0000000..d35c7fe --- /dev/null +++ b/spring-boot/spring-boot-common/src/main/java/com/wyl/spring/boot/common/aop/annotation/LogTest.java @@ -0,0 +1,10 @@ +package com.wyl.spring.boot.common.aop.annotation; + +import java.lang.annotation.*; + +@Target(ElementType.METHOD) +@Retention(RetentionPolicy.RUNTIME) +@Inherited//注解会被继承 +@Documented +public @interface LogTest { +} diff --git a/spring-boot/spring-boot-common/src/main/java/com/wyl/spring/boot/common/aop/aspect/LogAspect.java b/spring-boot/spring-boot-common/src/main/java/com/wyl/spring/boot/common/aop/aspect/LogAspect.java new file mode 100644 index 0000000..23ef6cd --- /dev/null +++ b/spring-boot/spring-boot-common/src/main/java/com/wyl/spring/boot/common/aop/aspect/LogAspect.java @@ -0,0 +1,65 @@ +package com.wyl.spring.boot.common.aop.aspect; + +import lombok.extern.slf4j.Slf4j; +import org.aspectj.lang.JoinPoint; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.*; +import org.springframework.stereotype.Component; + +/** + * 接口出入参数信息依赖swagger + * + * @author wangyl + * @version V1.0 + * @ClassName: WebLogAspect + * @Date: 2022/1/5 22:58 + */ +@Aspect +@Slf4j +@Component +public class LogAspect { + + /** + * 以自定义 @WebLog 注解为切点 + */ + @Pointcut("@annotation(com.wyl.spring.boot.common.aop.annotation.LogTest)") + public void webLog() { + } + + /** + * 在切点之前织入 + * + * @param joinPoint + * @throws Throwable + */ + @Before("webLog()") + public void doBefore(JoinPoint joinPoint) { + System.out.println("Aspect Before 打印"); + } + + /** + * 在切点之后织入 + * + * @throws Throwable + */ + @After("webLog()") + public void doAfter() { + System.out.println("Aspect After 打印"); + } + + /** + * 环绕 + * + * @param proceedingJoinPoint + * @return + * @throws Throwable + */ + @Around("webLog()") + public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { + System.out.println("Aspect 方法执行前打印 打印"); + Object proceed = proceedingJoinPoint.proceed(); + System.out.println("Aspect 方法执行后打印 打印"); + return proceed; + } + +} \ No newline at end of file diff --git a/spring-boot/spring-boot-common/src/main/java/com/wyl/spring/boot/common/bean/init/BeanInitExample.java b/spring-boot/spring-boot-common/src/main/java/com/wyl/spring/boot/common/bean/init/BeanInitExample.java new file mode 100644 index 0000000..f5ba7f5 --- /dev/null +++ b/spring-boot/spring-boot-common/src/main/java/com/wyl/spring/boot/common/bean/init/BeanInitExample.java @@ -0,0 +1,33 @@ +package com.wyl.spring.boot.common.bean.init; + +import org.springframework.beans.factory.DisposableBean; +import org.springframework.beans.factory.InitializingBean; + +import javax.annotation.PostConstruct; +import javax.annotation.PreDestroy; + +public class BeanInitExample implements InitializingBean, DisposableBean { + public BeanInitExample() { + System.out.println("构造函数被调用"); + } + + @PostConstruct + public void init() { + System.out.println("PostConstruct 被调用"); + } + + @PreDestroy + public void disposable() { + System.out.println("PreDestroy 被调用"); + } + + @Override + public void afterPropertiesSet() throws Exception { + System.out.println("InitializingBean 被调用"); + } + + @Override + public void destroy() throws Exception { + System.out.println("DisposableBean 被调用"); + } +} diff --git a/spring-boot/spring-boot-common/src/main/java/com/wyl/spring/boot/common/service/LogTestService.java b/spring-boot/spring-boot-common/src/main/java/com/wyl/spring/boot/common/service/LogTestService.java new file mode 100644 index 0000000..b1782d1 --- /dev/null +++ b/spring-boot/spring-boot-common/src/main/java/com/wyl/spring/boot/common/service/LogTestService.java @@ -0,0 +1,13 @@ +package com.wyl.spring.boot.common.service; + +import com.wyl.spring.boot.common.spel.bean.Order; + +import java.util.List; + +public interface LogTestService { + void test(); + + void testSPEL(Order order); + + void testSPELList(List orders); +} diff --git a/spring-boot/spring-boot-common/src/main/java/com/wyl/spring/boot/common/service/impl/LogTestServiceImpl.java b/spring-boot/spring-boot-common/src/main/java/com/wyl/spring/boot/common/service/impl/LogTestServiceImpl.java new file mode 100644 index 0000000..67c844d --- /dev/null +++ b/spring-boot/spring-boot-common/src/main/java/com/wyl/spring/boot/common/service/impl/LogTestServiceImpl.java @@ -0,0 +1,30 @@ +package com.wyl.spring.boot.common.service.impl; + +import com.wyl.spring.boot.common.aop.annotation.LogTest; +import com.wyl.spring.boot.common.service.LogTestService; +import com.wyl.spring.boot.common.spel.annotation.SpelTest; +import com.wyl.spring.boot.common.spel.bean.Order; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class LogTestServiceImpl implements LogTestService { + @Override + @LogTest + public void test() { + System.out.println("testLog"); + } + + + @SpelTest(spel = "{#order.id}") + public void testSPEL(Order order) { + System.out.println(123); + } + + @Override + @SpelTest(spel = "{#orders.![#this.id]}") + public void testSPELList(List orders) { + System.out.println(); + } +} diff --git a/spring-boot/spring-boot-common/src/main/java/com/wyl/spring/boot/common/spel/SpelTestAspect.java b/spring-boot/spring-boot-common/src/main/java/com/wyl/spring/boot/common/spel/SpelTestAspect.java new file mode 100644 index 0000000..04c318d --- /dev/null +++ b/spring-boot/spring-boot-common/src/main/java/com/wyl/spring/boot/common/spel/SpelTestAspect.java @@ -0,0 +1,61 @@ +package com.wyl.spring.boot.common.spel; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.wyl.spring.boot.common.spel.annotation.SpelTest; +import com.wyl.spring.boot.common.spel.service.EvaluationContext; +import com.wyl.spring.boot.common.spel.service.Evaluator; +import com.wyl.spring.boot.common.spel.service.RootObject; +import lombok.extern.slf4j.Slf4j; +import org.aspectj.lang.JoinPoint; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.Signature; +import org.aspectj.lang.annotation.*; +import org.aspectj.lang.reflect.MethodSignature; +import org.springframework.aop.framework.AopProxyUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.lang.reflect.Method; + +@Aspect +@Slf4j +@Component +public class SpelTestAspect { + /** + * 日志SpEL解析器 + */ + private final Evaluator evaluator = new Evaluator(); + + /** + * 日志切入点 + */ + @Pointcut(value = "@annotation(com.wyl.spring.boot.common.spel.annotation.SpelTest)") + public void SpelTestPointCut() { + } + + + @Around(value = "SpelTestPointCut()") + public Object errorLog(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { + proceedingJoinPoint.getArgs(); + Signature signature = proceedingJoinPoint.getSignature(); + MethodSignature methodSignature = (MethodSignature) signature; + Method method = methodSignature.getMethod(); + Object[] args = proceedingJoinPoint.getArgs(); + Class targetClass = AopProxyUtils.ultimateTargetClass(proceedingJoinPoint.getTarget()); + SpelTest annotation = method.getAnnotation(SpelTest.class); + /** + * 生成元数据 + */ + RootObject rootObject = new RootObject(); + /** + * 生成spel上下文 + */ + EvaluationContext context = new EvaluationContext(rootObject, method, args, evaluator.getDiscoverer()); + /** + * 解析spel + */ + Object content = evaluator.parse(annotation.spel(), context); + System.out.println(content); + return proceedingJoinPoint.proceed(); + } +} \ No newline at end of file diff --git a/spring-boot/spring-boot-common/src/main/java/com/wyl/spring/boot/common/spel/annotation/SpelTest.java b/spring-boot/spring-boot-common/src/main/java/com/wyl/spring/boot/common/spel/annotation/SpelTest.java new file mode 100644 index 0000000..3371e02 --- /dev/null +++ b/spring-boot/spring-boot-common/src/main/java/com/wyl/spring/boot/common/spel/annotation/SpelTest.java @@ -0,0 +1,15 @@ +package com.wyl.spring.boot.common.spel.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 SpelTest { + /** + * spel + */ + String spel(); +} \ No newline at end of file diff --git a/spring-boot/spring-boot-common/src/main/java/com/wyl/spring/boot/common/spel/bean/Order.java b/spring-boot/spring-boot-common/src/main/java/com/wyl/spring/boot/common/spel/bean/Order.java new file mode 100644 index 0000000..a833ba6 --- /dev/null +++ b/spring-boot/spring-boot-common/src/main/java/com/wyl/spring/boot/common/spel/bean/Order.java @@ -0,0 +1,9 @@ +package com.wyl.spring.boot.common.spel.bean; + +import lombok.Data; + +@Data +public class Order { + Integer id; + String name; +} diff --git a/spring-boot/spring-boot-common/src/main/java/com/wyl/spring/boot/common/spel/service/EvaluationContext.java b/spring-boot/spring-boot-common/src/main/java/com/wyl/spring/boot/common/spel/service/EvaluationContext.java new file mode 100644 index 0000000..7c76f50 --- /dev/null +++ b/spring-boot/spring-boot-common/src/main/java/com/wyl/spring/boot/common/spel/service/EvaluationContext.java @@ -0,0 +1,22 @@ +package com.wyl.spring.boot.common.spel.service; + +import org.springframework.context.expression.MethodBasedEvaluationContext; +import org.springframework.core.ParameterNameDiscoverer; + +import java.lang.reflect.Method; +/** + * @author: wangyl + * @date: 2022/5/8 + * @description: 解析上下文, 用于整个解析环境 + */ +public class EvaluationContext extends MethodBasedEvaluationContext { + /** + * 构造方法 + * + * @param rootObject 数据来源对象 + * @param discoverer 参数解析器 + */ + public EvaluationContext(RootObject rootObject, Method method, Object[] arguments, ParameterNameDiscoverer discoverer) { + super(rootObject, method, arguments, discoverer); + } +} \ No newline at end of file diff --git a/spring-boot/spring-boot-common/src/main/java/com/wyl/spring/boot/common/spel/service/Evaluator.java b/spring-boot/spring-boot-common/src/main/java/com/wyl/spring/boot/common/spel/service/Evaluator.java new file mode 100644 index 0000000..308be3f --- /dev/null +++ b/spring-boot/spring-boot-common/src/main/java/com/wyl/spring/boot/common/spel/service/Evaluator.java @@ -0,0 +1,52 @@ +package com.wyl.spring.boot.common.spel.service; + +import lombok.Getter; +import org.springframework.core.DefaultParameterNameDiscoverer; +import org.springframework.core.ParameterNameDiscoverer; +import org.springframework.expression.Expression; +import org.springframework.expression.ParserContext; +import org.springframework.expression.common.TemplateParserContext; +import org.springframework.expression.spel.standard.SpelExpressionParser; + +/** + * @author: wangyl + * @date: 2022/5/8 + * @description: spel解析处理器 + */ +@Getter +public class Evaluator { + /** + * SpEL解析器 + */ + private final SpelExpressionParser parser = new SpelExpressionParser(); + /** + * 参数解析器 + */ + private final ParameterNameDiscoverer discoverer = new DefaultParameterNameDiscoverer(); + /** + * 表达式模板 + */ + private final ParserContext template = new TemplateParserContext("{", "}"); + + /** + * 解析 + * + * @param expression 表达式 + * @param context 日志表达式上下文 + * @return 表达式结果 + */ + public Object parse(String expression, EvaluationContext context) { + return getExpression(expression).getValue(context); + } + + /** + * 获取翻译后表达式 + * + * @param expression 字符串表达式 + * @return 翻译后表达式 + */ + private Expression getExpression(String expression) { + return getParser().parseExpression(expression, template); + } + +} \ No newline at end of file diff --git a/spring-boot/spring-boot-common/src/main/java/com/wyl/spring/boot/common/spel/service/RootObject.java b/spring-boot/spring-boot-common/src/main/java/com/wyl/spring/boot/common/spel/service/RootObject.java new file mode 100644 index 0000000..299e607 --- /dev/null +++ b/spring-boot/spring-boot-common/src/main/java/com/wyl/spring/boot/common/spel/service/RootObject.java @@ -0,0 +1,15 @@ +package com.wyl.spring.boot.common.spel.service; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +/** + * @author: wangyl + * @date: 2022/5/8 + * @description: spel解析得元数据 + */ +@Getter +@AllArgsConstructor +public class RootObject { + +} \ No newline at end of file diff --git a/spring-boot/spring-boot-common/src/main/resources/application.properties b/spring-boot/spring-boot-common/src/main/resources/application.properties new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/spring-boot/spring-boot-common/src/main/resources/application.properties @@ -0,0 +1 @@ + diff --git a/spring-boot/spring-boot-common/src/test/java/com/wyl/spring/boot/common/SpringBootCommonApplicationTests.java b/spring-boot/spring-boot-common/src/test/java/com/wyl/spring/boot/common/SpringBootCommonApplicationTests.java new file mode 100644 index 0000000..fa8fcad --- /dev/null +++ b/spring-boot/spring-boot-common/src/test/java/com/wyl/spring/boot/common/SpringBootCommonApplicationTests.java @@ -0,0 +1,51 @@ +package com.wyl.spring.boot.common; + +import com.wyl.spring.boot.common.service.LogTestService; +import com.wyl.spring.boot.common.spel.bean.Order; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +import java.util.Arrays; +import java.util.List; + +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE) +class SpringBootCommonApplicationTests { + + @Autowired + LogTestService logTestService; + + @Test + void beanInit() { + } + + @Test + void LogTestServiceTest() { + logTestService.test(); + } + + @Test + void SpelTest() { + Order order = new Order(); + order.setId(123); + order.setName("wyl"); + logTestService.testSPEL(order); + } + + @Test + void SpelListTest() { + Order order1 = new Order(); + order1.setId(1); + order1.setName("wyl1"); + Order order2 = new Order(); + order2.setId(2); + order2.setName("wyl2"); + Order order3 = new Order(); + order3.setId(3); + order3.setName("wyl3"); + List orders = Arrays.asList(order1, order2, order3); + logTestService.testSPELList(orders); + } + + +} diff --git a/spring-boot/spring-boot-drools/src/test/java/com/wyl/example/AppTest.java b/spring-boot/spring-boot-drools/src/test/java/com/wyl/example/AppTest.java deleted file mode 100644 index 70b9b96..0000000 --- a/spring-boot/spring-boot-drools/src/test/java/com/wyl/example/AppTest.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.wyl.example; - -import static org.junit.Assert.assertTrue; - -import org.junit.Test; - -/** - * Unit test for simple App. - */ -public class AppTest -{ - /** - * Rigorous Test :-) - */ - @Test - public void shouldAnswerWithTrue() - { - assertTrue( true ); - } -} diff --git a/spring-boot/spring-boot-drools/src/test/java/com/wyl/example/service/Service.java b/spring-boot/spring-boot-drools/src/test/java/com/wyl/example/service/Service.java new file mode 100644 index 0000000..c38dcf7 --- /dev/null +++ b/spring-boot/spring-boot-drools/src/test/java/com/wyl/example/service/Service.java @@ -0,0 +1,5 @@ +package com.wyl.example.service; + +public interface Service { + void test(); +} diff --git a/spring-boot/spring-boot-drools/src/test/java/com/wyl/example/service/ServiceTest.java b/spring-boot/spring-boot-drools/src/test/java/com/wyl/example/service/ServiceTest.java new file mode 100644 index 0000000..ad08bba --- /dev/null +++ b/spring-boot/spring-boot-drools/src/test/java/com/wyl/example/service/ServiceTest.java @@ -0,0 +1,8 @@ +package com.wyl.example.service; + +public class ServiceTest implements Service{ + @Override + public void test() { + + } +}