SpringBoot学习笔记——浅谈AOP
来源:互联网 发布:淘宝买的lol号安全吗 编辑:程序博客网 时间:2024/06/04 19:50
浅谈AOP
AOP是一种编程范式,与语言无关,是一种编程思想。
- 面向切面(AOP)Aspect Oriented Programming
- 面向对象(OOP)Object Oriented Programming
- 面向过程(POP)Procedure Oriented Programming
面向过程与面向对象的区别:(换了个角度看世界,换了个姿势处理问题)
- 面相对象是垂直划分为不同的,并且相对独立的。封装成类,并且有自己的行为。
- 面相切面是一种横切的技术,将面向对象构成的体系进行水平切割;
并且把可能会影响到的多个类的公共行为封装成可重用的模块,这个模块称为(切面)。
核心思想: 将通用逻辑从业务中分离出来
@Aspect //切面
@Component //组件引入Spring容器中
//切点
@Pointcut(“execution(public * com.hsw.controller.GirlController.*(..))”)
//execution()执行括号里面的方法进行拦截 任意返回值,任意方法,任意参数
@Before()
@After()
package com.hsw.aspect;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.*;import org.hibernate.boot.jaxb.SourceType;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.stereotype.Component;import org.springframework.web.context.request.RequestContextHolder;import org.springframework.web.context.request.ServletRequestAttributes;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletRequestWrapper;import java.awt.*;/** * ArvinWoo * * 设置切面 切点提取 */@Aspect@Component //引入Spring容器中public class HttpAspect { private final static Logger logger = LoggerFactory.getLogger(HttpAspect.class); //execution()执行括号里面的方法进行拦截 任意返回值,任意方法,任意参数 @Pointcut("execution(public * com.hsw.controller.GirlController.*(..))") public void log(){ } @Before("log()") public void doBefore(JoinPoint joinPoint){//JoinPoint 获取请求方法信息 //HttpServletRequest 从 RequestContextHolder取得 ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest();// url 会把request请求变量放到url={}里面 logger.info("url={}",request.getRequestURL());// method logger.info("method={}",request.getMethod());// ip logger.info("ip={}",request.getRemoteAddr());// 类方法 logger.info("class_method={}",joinPoint.getSignature().getDeclaringTypeName()+"."+joinPoint.getSignature().getName()); //参数 logger.info("args={}",joinPoint.getArgs()); } @After("log()") public void doAfter(){ logger.info("···"); } //获取返回的内容 @AfterReturning(returning = "object", pointcut = "log()") public void doAfterRetruning(Object object){ logger.info("response={}",object.toString()); }}
遇到的错误
{ "timestamp": 1510495946064, "status": 500, "error": "Internal Server Error", "exception": "org.springframework.http.converter.HttpMessageNotWritableException", "message": "Could not write JSON: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS); nested exception is com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: com.hsw.domain.Girl_$$_jvst83b_0[\"handler\"])", "path": "/girls/13"}
根据提示,大致的意思应该是实体类在转化为json时,有属性值是null所致。
解决方法
在实体类上面加入:
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;@JsonIgnoreProperties({"handler","hibernateLazyInitializer" })
阅读全文
0 0
- SpringBoot学习笔记——浅谈AOP
- SpringBoot学习(四)—AOP切面编程
- SpringBoot学习笔记——Hello SpringBoot!
- 学习笔记——AOP
- SpringBoot学习笔记——数据库操作
- AOP学习笔记(二)——Spring AOP
- 03-SpringBoot——Spring基础-AOP
- Spring学习笔记3——AOP
- Spring学习笔记——AOP
- spring学习笔记肆——AOP
- 学习笔记09—代理和AOP
- SpringBoot学习——SpringBoot入门HelloWorld
- Java学习笔记—浅谈多线程
- SpringBoot学习笔记——项目属性配置
- SpringBoot学习笔记——多环境配置
- SpringBoot学习笔记——一些Spring注解
- SpringBoot 学习记录(五): aop记录日志
- (7)学习SpringBoot 之 AOP
- ExtJs之下拉框ComboBox
- markdown语法之数学符号汇总
- 新增其他常用元素
- mybatis的批量删除
- Effective STL学习笔记-条款33
- SpringBoot学习笔记——浅谈AOP
- 如何查看ELF文件
- 设计模式--工厂模式
- 【总结】NOIP2017 提高组
- ntfs-3g MacOS High Sierra 10.13.1 brew编译失败解决方案
- 实验一:写一个hello world小程序
- [c/c++]c语言经典之求最大公约数的3中简单方法(速成)
- 批处理之优盘中毒处理
- 一、Linux内核简介