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" })