From 1f307777789f9faf2111be84bcddf14576398289 Mon Sep 17 00:00:00 2001 From: qinyingjie Date: Mon, 19 Dec 2022 16:31:42 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E9=85=8D=E7=BD=AE=E6=8B=A6=E6=88=AA?= =?UTF-8?q?=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 22 +++++++- .../kwan/springbootkwan/aop/LogAspect.java | 51 +++++++++++++++++++ .../config/DataSourceConfig.java | 24 +++++++++ .../springbootkwan/config/MyInterceptor.java | 33 ++++++++++++ .../springbootkwan/config/MyWebMvcConfig.java | 25 ++++++++- 5 files changed, 152 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/kwan/springbootkwan/aop/LogAspect.java create mode 100644 src/main/java/com/kwan/springbootkwan/config/DataSourceConfig.java create mode 100644 src/main/java/com/kwan/springbootkwan/config/MyInterceptor.java diff --git a/pom.xml b/pom.xml index 10de49d..5b0293a 100644 --- a/pom.xml +++ b/pom.xml @@ -74,8 +74,15 @@ spring-boot-devtools true - - + + org.springframework.boot + spring-boot-starter-aop + + + com.alibaba + druid-spring-boot-starter + 1.1.10 + @@ -92,5 +99,16 @@ + + + src/main/java + + **/*.xml + + + + src/main/resources + + \ No newline at end of file diff --git a/src/main/java/com/kwan/springbootkwan/aop/LogAspect.java b/src/main/java/com/kwan/springbootkwan/aop/LogAspect.java new file mode 100644 index 0000000..45e6cc3 --- /dev/null +++ b/src/main/java/com/kwan/springbootkwan/aop/LogAspect.java @@ -0,0 +1,51 @@ +package com.kwan.springbootkwan.aop; + +import org.aspectj.lang.JoinPoint; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.*; +import org.springframework.stereotype.Component; + +/** + * AOP 实现方法拦截 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2022/12/19 16:17 + */ +@Component +@Aspect +public class LogAspect { + + @Pointcut(value = "execution(* com.kwan.springbootkwan.service.*.*(..))") + public void pcl() { + } + + @Before(value = "pcl()") + public void before(JoinPoint point) { + String name = point.getSignature().getName(); + System.out.println(name + "方法开始执行 ..."); + } + + @After(value = "pcl()") + public void after(JoinPoint point) { + String name = point.getSignature().getName(); + System.out.println(name + "方法执行结束 ..."); + } + + @AfterReturning(value = "pcl()", returning = "result") + public void afterReturning(JoinPoint point, Object result) { + String name = point.getSignature().getName(); + System.out.println(name + "方法返回值为:" + result); + } + + @AfterThrowing(value = "pcl()", throwing = "e") + public void afterThrowing(JoinPoint point, Exception e) { + String name = point.getSignature().getName(); + System.out.println(name + "方法抛异常了,异常是:" + e.getMessage()); + } + + @Around("pcl()") + public Object around(ProceedingJoinPoint pjp) throws Throwable { + return pjp.proceed(); + } +} diff --git a/src/main/java/com/kwan/springbootkwan/config/DataSourceConfig.java b/src/main/java/com/kwan/springbootkwan/config/DataSourceConfig.java new file mode 100644 index 0000000..bd4a6e7 --- /dev/null +++ b/src/main/java/com/kwan/springbootkwan/config/DataSourceConfig.java @@ -0,0 +1,24 @@ +package com.kwan.springbootkwan.config; + +import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import javax.sql.DataSource; + +@Configuration +public class DataSourceConfig { + + @Bean + @ConfigurationProperties("spring.datasource.one") + DataSource dataSourceOne() { + return DruidDataSourceBuilder.create().build(); + } + + @Bean + @ConfigurationProperties("spring.datasource.two") + DataSource dataSourceTwo() { + return DruidDataSourceBuilder.create().build(); + } +} diff --git a/src/main/java/com/kwan/springbootkwan/config/MyInterceptor.java b/src/main/java/com/kwan/springbootkwan/config/MyInterceptor.java new file mode 100644 index 0000000..9b0d45c --- /dev/null +++ b/src/main/java/com/kwan/springbootkwan/config/MyInterceptor.java @@ -0,0 +1,33 @@ +package com.kwan.springbootkwan.config; + +import org.springframework.web.servlet.HandlerInterceptor; +import org.springframework.web.servlet.ModelAndView; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +/** + * 自定义拦截器 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2022/12/19 16:14 + */ +public class MyInterceptor implements HandlerInterceptor { + @Override + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { + System.out.println("MyInterceptor>>>>>>>>>preHandle"); + return true; + } + + @Override + public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { + System.out.println("MyInterceptor>>>>>>>>>postHandle"); + } + + @Override + public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { + System.out.println("MyInterceptor>>>>>>>>>afterCompletion"); + } +} + diff --git a/src/main/java/com/kwan/springbootkwan/config/MyWebMvcConfig.java b/src/main/java/com/kwan/springbootkwan/config/MyWebMvcConfig.java index e3a9ed1..fcab5b2 100644 --- a/src/main/java/com/kwan/springbootkwan/config/MyWebMvcConfig.java +++ b/src/main/java/com/kwan/springbootkwan/config/MyWebMvcConfig.java @@ -2,11 +2,12 @@ package com.kwan.springbootkwan.config; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.CorsRegistry; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; /** - * 全局拦截器-跨域配置 + * 全局webconfig-跨域配置 * * @author : qinyingjie * @version : 2.2.0 @@ -14,12 +15,34 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; */ @Configuration public class MyWebMvcConfig implements WebMvcConfigurer { + /** + * 配置静态资源 + * + * @param registry + */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/static/**") .addResourceLocations("classpath:/static/"); } + /** + * 配置拦截器 + * + * @param registry + */ + @Override + public void addInterceptors(InterceptorRegistry registry) { + registry.addInterceptor(new MyInterceptor()) + .addPathPatterns("/**") + .excludePathPatterns("/hello"); + } + + /** + * 配置全局跨域 + * + * @param registry + */ @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/book/**") -- GitLab