diff --git a/pom.xml b/pom.xml index 10de49d1d7cc8699d4c9001a5f85da0d6859a4bc..5b0293a13e1b10e14e0adce527e5abb7a1e700f8 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 0000000000000000000000000000000000000000..45e6cc36ca5c632b398ee9546e4d78ebcfde7f4a --- /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 0000000000000000000000000000000000000000..bd4a6e7e73109833e92f145a12f686fa663b6669 --- /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 0000000000000000000000000000000000000000..9b0d45cb18325a2acc0d1bdc04e8cdc5b4d1b516 --- /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 e3a9ed16c35a6b0de8fb30cee97b0709a9ddabf2..fcab5b2570b6643d56855d58dba528dec6860d1f 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/**")