轻松qinsong

开发中遇到的技术、方法

SpringBoot-AOP

1.jar包

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

2.添加apo的方法

package com.gdy.springboot1;

import com.gdy.springboot1.domain.User;
import com.gdy.springboot1.domain.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/user/")
public class UserController {

    @Autowired
    private User user;

    @Autowired
    private UserDao userDao;

    @RequestMapping("find")
    public String getUser(){
        return user.toString();
    }

    /**
     * 查找所有用户信息
     * @return
     */
    @RequestMapping("findAlls")
    public List<UserInfo> findAlls(){
        return userDao.findAll();
    }

    /**
     * 根据id查找用户信息
     * @return
     */
//    @GetMapping("find/{id}")
//    public UserInfo findAlls(@PathVariable("id") Integer id){
//        return userDao.findOne(id);
//    }



    /**
     * 现在用户信息
     *
     * @return
     */
    @RequestMapping("addUser")
    //对参数进行验证
    public UserInfo addUser(@Valid UserInfo userInfo , BindingResult bindResult){

        if(bindResult.hasErrors()){
            System.out.println(bindResult.getFieldError().getDefaultMessage());
            return null;
        }

        return userDao.save(userInfo);
    }


}

3.AOP

package com.gdy.springboot1.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
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;

@Aspect
@Component
public class HttpAspect {

    private final static Logger log = LoggerFactory.getLogger(HttpAspect.class);


    /**
     * 定义切点 controller下的所有方法
     */
    @Pointcut("execution(public * com.gdy.springboot1.UserController.*(..))")
    public void log(){};


    /**
     * 方法前执行
     * @param joinPoint
     */
    @Before("log()")  //引用定义的切点
    public void beforLog(JoinPoint joinPoint){
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        //url
        log.info("url:{}",request.getRequestURL());
        //ip
        log.info("ip:{}",request.getRemoteHost());
        //method
        log.info("请求类型:{}",request.getMethod());
        //类方法
        //joinPoint.getSignature().getDeclaringTypeName() -- 类名
        //joinPoint.getSignature().getName() -- 方法名
        log.info("类方法:{}", joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
        //参数
        log.info("参数:{}"+joinPoint.getArgs());
    }

    /**
     * 方法后执行
     */
    @After("log()")
    public void afterLog(){
        System.out.println("after aop");
    }


    /**
     *   获取方法返回值
     */

    @AfterReturning(pointcut = "log()",returning = "object")
    public void afterReturning(Object object){
        log.info("response:{}",object);
    }


}

这里写图片描述

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014136910/article/details/79955068
个人分类: 框架-SpringBoot
想对作者说点什么? 我来说一句

springboot-AOP

springboot-AOP

Inke88 Inke88

2017-07-07 18:52:50

阅读数:270

没有更多推荐了,返回首页

不良信息举报

SpringBoot-AOP

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭