Springboot以及aop面向切面编程

 在Springboot中实现一个简单的aop
(1)引入aop的依赖
要确保maven项目中已经添加了springboot的依赖。

<!--spring切面aop依赖-->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-aop</artifactId>
</dependency>


(2)日志要记录在数据库中,要有对应的bean实体类

public class Operation {
    private Long   id;
    private String identity ; // 操作人账号
    private String clientIp ; // 客户端ip
    private String username ; // 操作人姓名
    private Long   operType ; // 日志类型 
    private String operUrl ; // 操作的url 
    private String operEvent ; // 操作事件
    private String reqParam ; // 请求参数信息
    private String reqType ; // 请求方式:POST或者GET
    private Date   operTime ; // 操作时间    
    //提供set和get方法
}


(3)自定义一个注解类,也就是一个切入点

@Target(ElementType.METHOD) //注解放置的目标位置,METHOD是可注解在方法级别上
@Retention(RetentionPolicy.RUNTIME) //注解在哪个阶段执行
@Documented //生成文档
public @interface ViLog {
    /** 操作事件     */
    String operEvent () default "";

    /** 日志类型 */
    int operType ();
}


(4)定义一个切面类,进行具体的记录工作

/** 系统日志:切面处理类 */
@Aspect
@Component
public class SysLogAspect {
    private static final Logger log = LoggerFactory.getLogger(SysLogAspect.class);

    @Autowired
    private IOperationService operationService;

    //定义切点 @Pointcut
    //在注解的位置切入代码
    @Pointcut("@annotation(自定义注解类的全路径)")
    public void logPoinCut() {}

    //切面 配置通知
    @Before("logPoinCut()")
    public void saveOperation(JoinPoint joinPoint) {
       //保存日志操作
    }
}


(5)在需要检测的接口添加注解

//例如在contoller类的方法上加注解
@RestController
@RequestMapping("/sysMenuController")
public class SysMenuController extends AbstractController {
    @ViidLog(operEvent = "删除菜单记录")//定义织入点
    @PostMapping("/del")
    public void deleteBatch(@RequestBody Long[] menuIds) {
    }
}


简单的切入点、织入点都在注释中和类结合起来说明了,如果只看概念可能有些抽象,这样结合代码说明更形象一些了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot实现AOP面向切面编程的具体方法如下: 1. 首先,你需要在项目的pom.xml文件中添加spring-boot-starter-aop依赖。可以参考以下代码: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> ``` 2. 然后,你需要编写一个用于拦截的bean。这个bean将包含你想要在目标方法执行前后执行的逻辑。你可以使用注解或者编程方式来定义切面。例如,你可以使用@Aspect注解来定义一个切面,然后在切面的方法上使用@Before、@After等注解来定义具体的拦截行为。 3. 接下来,你需要将切面应用到目标对象上,创建代理对象。这个过程称为织入(Weaving)。在Spring Boot中,你可以使用@EnableAspectJAutoProxy注解来启用自动代理,它会根据切面定义自动创建代理对象。 总而言之,Spring Boot实现AOP面向切面编程的具体方法包括:添加依赖、编写用于拦截的bean,以及启用自动代理。这样就能实现在目标方法执行前后执行特定逻辑的效果了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [SpringBoot整合aop面向切面编程过程解析](https://download.csdn.net/download/weixin_38689551/12743012)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [SpringBoot实现AOP面向切面编程](https://blog.csdn.net/weixin_52536274/article/details/130375560)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值