SpringBoot aop demo
来源:互联网 发布:mac版百度网盘模糊 编辑:程序博客网 时间:2024/06/06 00:59
package jyl.mix.springbootaop.aop;import com.google.gson.Gson;import java.util.Arrays;import javax.servlet.http.HttpServletRequest;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import org.springframework.web.context.request.RequestContextHolder;import org.springframework.web.context.request.ServletRequestAttributes;/** * 打日志操作 * 定义Aspect */@Aspect@Componentpublic class LogAop { private Logger logger = LoggerFactory.getLogger(this.getClass()); private static final String REQUEST_LOG = "request[url:{}, method:{}, clientIp:{}, params:{}]"; @Autowired private Gson gson; /** * 定义PointCut 即jyl.mix.springbootaop.controller包内的所有public方法 */ @Pointcut("execution(public * jyl.mix.springbootaop.controller..*.*(..))") public void URLlog() { } /** * 切点方法执行前进行的操作 * 此处打印请求的各种参数 * @param joinPoint */ @Before("URLlog()") public void logBefore(JoinPoint joinPoint) { HttpServletRequest request = (((ServletRequestAttributes) RequestContextHolder .getRequestAttributes())).getRequest(); logger.info(REQUEST_LOG, request.getRequestURL().toString(), request.getMethod(), request.getRemoteAddr(), Arrays.toString(joinPoint.getArgs())); } /** * 方法返回后执行的内容 * @param returnObj */ @AfterReturning(returning = "returnObj", pointcut = "URLlog()") public void logAfter(Object returnObj) { logger.info("response : " + gson.toJson(returnObj)); }}
package jyl.mix.springbootaop.controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class DemoAController { @RequestMapping("/demo/a") public String showA(){ return "demoA"; }}
package jyl.mix.springbootaop.controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class DemoBController { @RequestMapping("/demo/b") public String showA(){ return "demoB"; }}
package jyl.mix.springbootaop;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;@SpringBootApplicationpublic class AopApplicationContext implements EmbeddedServletContainerCustomizer { @Value("8087") private int port; public static void main(String[] args) { SpringApplication.run(AopApplicationContext.class, args); } @Override public void customize(ConfigurableEmbeddedServletContainer container) { container.setPort(port); }}
浏览器分别访问http://127.0.0.1:8087/demo/a 与http://127.0.0.1:8087/demo/b
可以观察到日志被成功打印出
阅读全文
0 0
- SpringBoot aop demo
- springboot-AOP
- SpringBoot-AOP
- springBoot aop
- AOP Demo
- springboot-aop访问拦截
- Springboot配置aop
- 第四十章 SpringBoot AOP
- springboot集成AOP
- springboot使用AOP
- SpringBoot-10整合AOP
- springboot之AOP配置
- springboot-AOP
- SpringBoot-AOP处理请求
- SpringBoot(五):aop
- Springboot与Aop
- SpringBoot+Gradle实现AOP
- springboot 集成aop
- 分享自己的几点
- 习题8-4 报数
- Linux下mysql的安装和配置
- 自动柜员机 C++
- 34STL之容器小结
- SpringBoot aop demo
- Java代码优化
- 网易云音乐搜索引擎 python+whoosh---(2)搜索引擎
- css3设置边框阴影的属性
- C/C++中的switch/case陷阱
- 有一个姑娘,我无法忘记她
- Excel省市区三级联动
- Linux--进程间的通信之共享内存
- 你要是学习能力强,6年前年学as,3年前学cocos,去年学unity,月薪16k算低