springboot的aop编程
以下内容是模仿杨开振<<深入浅出springboot 2.x>>的4.2章节内容。
开始前,需要先修改pom.xml,加入以下内容
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.1</version>
</dependency>
后面是需要逐一天增加或者修改的文件内容
- Note.java
- NoteService.java
- NoteServiceImpl.java
- NoteAspect.java
- NoteController.java
- App.java
Note.java
package study.spring.iocaop;
public class Note {
private String logDay;
private String keyWords;
private String contents;
private String title;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getLogDay() {
return logDay;
}
public void setLogDay(String logDay) {
this.logDay = logDay;
}
public String getKeyWords() {
return keyWords;
}
public void setKeyWords(String keyWords) {
this.keyWords = keyWords;
}
public String getContents() {
return contents;
}
public void setContents(String contents) {
this.contents = contents;
}
}
NoteService.java
package study.spring.iocaop;
public interface NoteService {
public void add(Note note);
public void print(Note note) throws Exception;
}
NoteServiceImpl.java
package study.spring.iocaop; import org.springframework.stereotype.Component; @Component
public class NoteServiceImpl implements NoteService { @Override
public void add(Note note) {
System.out.println(note.getTitle());
} @Override
public void print(Note note) throws Exception {
System.out.println("Title:"+note.getTitle());
System.out.println("day:"+note.getLogDay());
System.out.println("keyword:"+note.getKeyWords());
System.out.println("content:"+note.getContents());
throw new Exception("异常测试");
} }
NoteAspect.java
package study.spring.iocaop; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
//如果这里使用Component注解,后面的App.java中就不需要使用@Bean注解
@Component
@Aspect
public class NoteAspect { @Pointcut("execution(* study.spring.iocaop.NoteServiceImpl.print(..))")
public void pointCut(){ } /**
* 在切入函数中获取方法的参数
* @param point
* @param note
*/
@Before("pointCut() && args(note)")
public void before(JoinPoint point,Note note){
for(Object obj:point.getArgs()){ System.out.println("aop:"+obj.getClass().getName());
System.out.println("aop-target:"+point.getTarget().getClass().getName());
System.out.println(point.getThis().toString());
}
} @After("pointCut()")
public void after(){
System.out.println("aop:after note");
} @AfterReturning("pointCut()")
public void afterReturning(){
System.out.println("aop:afterReturning note");
} @AfterThrowing("pointCut()")
public void afterThrowing(){
System.out.println("aop:afterThrowing note");
}
}
NoteController.java
package study.spring.iocaop; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController; @RestController
public class NoteController {
@Autowired
NoteService noteService; @RequestMapping("/note/print")
@ResponseBody
public Note printNote() throws Exception{
Note note=new Note();
note.setTitle("在上海奋斗!");
note.setLogDay("2023-12-31");
note.setKeyWords("努力,科技,希望");
note.setContents("奋斗中......");
noteService.print(note);
return note;
}
}
App.java
package study; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean; import study.config.Myfilter;
import study.spring.iocaop.NoteAspect; /**
* Hello world!
*
*/
@SpringBootApplication
@ServletComponentScan
public class App extends SpringBootServletInitializer
{
public static void main(String[] args) {
SpringApplication.run(App.class, args);
} @Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
builder.sources(App.class);
return builder;
}
//这个定理aop bean,否则无法产生切入效果
@Bean(name="noteAspect")
public NoteAspect initNoteAspect(){
return new NoteAspect();
} @Bean
public FilterRegistrationBean<Myfilter> filterRegistrationBean() {
FilterRegistrationBean<Myfilter> bean = new FilterRegistrationBean<>();
bean.addUrlPatterns("/*");
bean.setFilter(new Myfilter());
return bean;
}
}
上面的例子中,如果不想在App中通过@Bean来生成NoteAspect的bean,也可以在NoteAspect的类的前面添加@Component
aop编程,在某些方面挺好用,例如记录日志,或者是设计一些底层的框架。
从设计思路和某些方面来说,不错!
springboot的aop编程的更多相关文章
- 记录一次SpringBoot实现AOP编程
需求 最近碰到一个问题,需要对关键操作的入参和返回值进行记录,并不是使用log记录,而是插入到数据库中. 思路:如果采用硬编码,在每个操作后都添加,会产生大量重复代码.因而打算使用自定义注解,通过AO ...
- SpringBoot学习笔记(七):SpringBoot使用AOP统一处理请求日志、SpringBoot定时任务@Scheduled、SpringBoot异步调用Async、自定义参数
SpringBoot使用AOP统一处理请求日志 这里就提到了我们Spring当中的AOP,也就是面向切面编程,今天我们使用AOP去对我们的所有请求进行一个统一处理.首先在pom.xml中引入我们需要的 ...
- SpringBoot切面Aop的demo简单讲解
前言 本篇文章主要介绍的是SpringBoot切面Aop的demo简单讲解. SpringBoot Aop 说明:如果想直接获取工程那么可以直接跳到底部,通过链接下载工程代码. 切面(Aop) 一.概 ...
- Spring全家桶——SpringBoot之AOP详解
Spring全家桶--SpringBoot之AOP详解 面向方面编程(AOP)通过提供另一种思考程序结构的方式来补充面向对象编程(OOP). OOP中模块化的关键单元是类,而在AOP中,模块化单元是方 ...
- 【原】iOS动态性(三) Method Swizzling以及AOP编程:在运行时进行代码注入
概述 今天我们主要讨论iOS runtime中的一种黑色技术,称为Method Swizzling.字面上理解Method Swizzling可能比较晦涩难懂,毕竟不是中文,不过你可以理解为“移花接木 ...
- 使用spring方式来实现aop编程
1:什么是aop? Aspect Oriented Programming 面向切面编程 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译 ...
- Spring学习笔记之四----基于Annotation的Spring AOP编程
你能使用@Aspect annotation将某个Java类标注为Aspect,这个Aspect类里的所有公有方法都可以成为一个Advice,Spring提供了5个Annotation去将某个方法标注 ...
- 聊Javascript中的AOP编程
Duck punch 我们先不谈AOP编程,先从duck punch编程谈起. 如果你去wikipedia中查找duck punch,你查阅到的应该是monkey patch这个词条.根据解释,Mon ...
- 基于ASP.NET MVC的热插拔模块式开发框架(OrchardNoCMS)--AOP编程
AOP编程在目前来说好像是大家都比较喜欢的.ASP.NET MVC中的Filter就是使用AOP实现的配置器模式.AOP在编码中的应用主要有如下几个方面: 日志记录,跟踪,优化和监控 事务的处理 持久 ...
随机推荐
- python30题
1.执行Python 脚本的两种方式 使用python解释器(python aa.py)或在unix系统下赋值成777,执行(./aa.py) 2.简述位.字节的关系 1个byte = 8bit,在A ...
- python调用其他文件的类和函数
在同一个文件夹下 调用函数 source.py文件: def func(): pass new.py文件: import source # 或者 from source import func 调用类 ...
- Python中and和or的运算法则
1. 在纯and语句中,如果每一个表达式都不是假的话,那么返回最后一个,因为需要一直匹配直到最后一个.如果有一个是假,那么返回假2. 在纯or语句中,只要有一个表达式不是假的话,那么就返回这个表达式的 ...
- angular2-响应式表单
响应式表单是同步的.模板驱动表单是异步的.这个不同点很重要 使用响应式表单,我们会在代码中创建整个表单控件树. 我们可以立即更新一个值或者深入到表单中的任意节点,因为所有的控件都始终是可用的. 模板驱 ...
- 【Mood-15】DailyBuild 1月
keywords: AsyncImageLoader universal-image-loader 2015-01-07 AsyncImageLoader:异步动态加载网络图片 类似listview ...
- Android应用开发基础之七:广播与服务(一)
广播 广播的概念 现实:电台通过发送广播发布消息,买个收音机,就能收听 Android:系统在产生某个事件时发送广播,应用程序使用广播接收者接收这个广播,就知道系统产生了什么事件. Android系统 ...
- ubuntu 18 环境下使用 @vue-cli 3.2 新建 vue 项目
ubuntu 18 环境下使用 @vue-cli 3.2 新建 vue 项目 标签(空格分隔): Vue 首先安装全局@vue-cli工具: npm install -g @vue/cli 然后创建项 ...
- Windows 2012R2远程桌面服务部署环境简介
一.部署环境 服务器名 所属域 IP地址 服务器用途 备注 AD01 CONTOSO.COM 192.168.1.1 域控制器 采用Windows Server 2012 R2 Datacenter ...
- 林锐:5 C++/C程序的基本概念
5.1.1 main 不能重载 不能内联 不能定义为static 不能取其地址 不能由用户直接调用 5.1.3内部名称 struct Sample_1 { int count; }; struct S ...
- 从产品展示页面谈谈Hybris的特有概念和设计结构
今天这篇文章来自我的同事,SAP成都研究院Hybris开发团队的开发人员Zhang Jonathan(张健).需要特别介绍的是,张健和成都研究院的其他开发同事不同,张健毕业于电子科技大学,读的专业是英 ...