springAOP

https://www.cnblogs.com/micrari/p/7612962.html

https://blog.csdn.net/lz710117239/article/details/78787280

AOP:AOP是面向切面编程,典型的应用就是Spring的声明式事务:

AOP的使用已经非常广泛了,它擅长于将多个类中共同使用到的方法横切出来,封装成某个模块,

如何在springboot中使用AOP:

1、导入pom依赖

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

2、编写切面类

package com.nxz.testboot.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect//表示当前类是切面类
@Component
@Slf4j
public class MethodExecTimeAspect {

    //在userService所有的方法执行之前和之后输出时间
    @Pointcut("execution(public * com.nxz.testboot.service.UserService.*(..))")   //切点表达式 
    //@Pointcut("@annotation(com.springboot2.spring5.springAop.aspect.Logable)") //根据注解切入
   public void pointCut() { } @Before(
"pointCut()") //方法之前执行 public void before(JoinPoint joinPoint) { log.info("方法开始输出,{},###,{}", System.currentTimeMillis(), joinPoint.getSignature()); } @After("pointCut()") //方法之后执行 public void after(JoinPoint joinPoint) { log.info("方法结束输出,{},###,{}", System.currentTimeMillis(), joinPoint.getSignature()); } }

3、执行userService测试方法是输出:

package com.nxz.testboot.service;

import com.nxz.testboot.dao.UserDao;
import com.nxz.testboot.domain.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.framework.AopContext;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
public class UserService {

    @Autowired
    private UserDao userDao;


    @Transactional(propagation = Propagation.REQUIRED)
    public void insertUser1(){
        User user = new User("niu","男",19,"1000000");
        userDao.insertUser(user);

        boolean isProxy = AopUtils.isAopProxy((UserService)AopContext.currentProxy());
        ((UserService)AopContext.currentProxy()).insertUser2();
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void insertUser2(){
        User user = new User("xing","女",19,"1111111");
        userDao.insertUser(user);
    }
}

结果:

c.nxz.testboot.aop.MethodExecTimeAspect  : 方法开始输出,1552226312788,###,void com.nxz.testboot.service.UserService.insertUser1()
c.nxz.testboot.aop.MethodExecTimeAspect  : 方法开始输出,1552226313102,###,void com.nxz.testboot.service.UserService.insertUser2()
c.nxz.testboot.aop.MethodExecTimeAspect  : 方法结束输出,1552226313103,###,void com.nxz.testboot.service.UserService.insertUser2()
c.nxz.testboot.aop.MethodExecTimeAspect  : 方法结束输出,1552226313129,###,void com.nxz.testboot.service.UserService.insertUser1()

 

spring aop源码解析:https://blog.csdn.net/woshilijiuyi/article/details/83934407

posted @ 2019-03-10 22:24  xj-record  阅读(150)  评论(0编辑  收藏  举报