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在编码中的应用主要有如下几个方面: 日志记录,跟踪,优化和监控 事务的处理 持久 ...
随机推荐
- 1、Dubbo源码解析--Dubbo如何驱动Spring IOC容器并配合工作的?
首先Spring要注入自己的bean需要在Spring-provider.xml(提供者spring注入文件,名字可能不一样)添加bean注入,其中有dubbo的自定义标签,xml如何识别这些标签?拿 ...
- scss-#{}插值
一般我们定义的变量都为属性值,可直接使用,但是如果变量作为属性或在某些特殊情况下则必须要以 #{$variables} 形式使用. 例如:scss代码 $borderDirection: top !d ...
- SQLAlchemy的使用---数据库的创建与连接
# 1. 导入SQLAlchemy from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import Col ...
- CSS3嵌入字体
@font-face能够加载服务器端的字体文件,让浏览器端可以显示用户电脑里没有安装的字体.语法: 例子:
- Windows 2012R2远程桌面服务简介
一.远程桌面服务概述 远程桌面服务加快并扩展了到任何设备的桌面和应用程序部署,在帮助保护关键知识产权的安全的同时提高了工作人员的工作效率,简化了法规遵从性. 远程桌面服务启用虚拟机基础结构 (VDI) ...
- 【转】vector中对象指针的排序
原文:http://blog.csdn.net/tanlijun37/article/details/1948493 vector中对象指针的排序,初步想法是1: 把对象指针存到vector,重载bo ...
- HDU 2048 错排
错排递推公式: d(n) = (n-1)*(d[n-1]+d[n-2]): 证明:将第n个元素放到第k处,第k处的元素如果放到第n处,就是d(n-2),否则,先假设放到第n处,然后错排,就是d(n-1 ...
- 【[NOI2013]矩阵游戏】
我们看到了及其可怕的数据范围 这个样子都没有办法直接读入的数据范围应该怎么算 我们观察一下递推式\(f[i][j]=a*f[i][j]+b(j!=1)\) \(f[i][1]=c*f[i-1][m]+ ...
- html题型
1.doctype的意义是什么 这个是有历史背景的,在很久以前,IE有一些自己的渲染模式,最典型的就是盒子模型,包括边距.这就造成了不兼容模式,所以他的意义 1)让浏览器以标准模式渲染 2)让浏览器知 ...
- POJ 2195 Going Home 【二分图最小权值匹配】
传送门:http://poj.org/problem?id=2195 Going Home Time Limit: 1000MS Memory Limit: 65536K Total Submis ...