以下内容是模仿杨开振<<深入浅出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>

后面是需要逐一天增加或者修改的文件内容

  1. Note.java
  2. NoteService.java
  3. NoteServiceImpl.java
  4. NoteAspect.java
  5. NoteController.java
  6. 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编程的更多相关文章

  1. 记录一次SpringBoot实现AOP编程

    需求 最近碰到一个问题,需要对关键操作的入参和返回值进行记录,并不是使用log记录,而是插入到数据库中. 思路:如果采用硬编码,在每个操作后都添加,会产生大量重复代码.因而打算使用自定义注解,通过AO ...

  2. SpringBoot学习笔记(七):SpringBoot使用AOP统一处理请求日志、SpringBoot定时任务@Scheduled、SpringBoot异步调用Async、自定义参数

    SpringBoot使用AOP统一处理请求日志 这里就提到了我们Spring当中的AOP,也就是面向切面编程,今天我们使用AOP去对我们的所有请求进行一个统一处理.首先在pom.xml中引入我们需要的 ...

  3. SpringBoot切面Aop的demo简单讲解

    前言 本篇文章主要介绍的是SpringBoot切面Aop的demo简单讲解. SpringBoot Aop 说明:如果想直接获取工程那么可以直接跳到底部,通过链接下载工程代码. 切面(Aop) 一.概 ...

  4. Spring全家桶——SpringBoot之AOP详解

    Spring全家桶--SpringBoot之AOP详解 面向方面编程(AOP)通过提供另一种思考程序结构的方式来补充面向对象编程(OOP). OOP中模块化的关键单元是类,而在AOP中,模块化单元是方 ...

  5. 【原】iOS动态性(三) Method Swizzling以及AOP编程:在运行时进行代码注入

    概述 今天我们主要讨论iOS runtime中的一种黑色技术,称为Method Swizzling.字面上理解Method Swizzling可能比较晦涩难懂,毕竟不是中文,不过你可以理解为“移花接木 ...

  6. 使用spring方式来实现aop编程

    1:什么是aop? Aspect Oriented Programming 面向切面编程 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译 ...

  7. Spring学习笔记之四----基于Annotation的Spring AOP编程

    你能使用@Aspect annotation将某个Java类标注为Aspect,这个Aspect类里的所有公有方法都可以成为一个Advice,Spring提供了5个Annotation去将某个方法标注 ...

  8. 聊Javascript中的AOP编程

    Duck punch 我们先不谈AOP编程,先从duck punch编程谈起. 如果你去wikipedia中查找duck punch,你查阅到的应该是monkey patch这个词条.根据解释,Mon ...

  9. 基于ASP.NET MVC的热插拔模块式开发框架(OrchardNoCMS)--AOP编程

    AOP编程在目前来说好像是大家都比较喜欢的.ASP.NET MVC中的Filter就是使用AOP实现的配置器模式.AOP在编码中的应用主要有如下几个方面: 日志记录,跟踪,优化和监控 事务的处理 持久 ...

随机推荐

  1. 使用durid的ConfigFilter对数据库密码加密

    <!-- 配置dbcp数据源 --> <bean id="remoteDS" class="org.apache.commons.dbcp.BasicD ...

  2. WinSock WSAEventSelect 模型

    在前面我们说了WSAAsyncSelect 模型,它相比于select模型来说提供了这样一种机制:当发生对应的IO通知时会立即通知操作系统,并调用对应的处理函数,它解决了调用send和 recv的时机 ...

  3. linux注册服务教程

    该说明是项目完成很久之后,整理资料时的偶然发现,当时所操作的linux为中标麒麟,需要对项目进行开机自启,对llinux还不熟悉,找不到linux中的服务自启设置.辗转多次才找到了解决方案.记录以供参 ...

  4. matlab练习程序(模糊集图像增强)

    算法有很多变种.不过主要就是以下三步. 1.设计隶属度函数将图像从空间域变换到模糊集域. 2.设计模糊增强算子,在模糊集域对图像进行处理. 3.根据第1步的隶属度函数重新将图像从模糊集域变换到空间域. ...

  5. matlab练习程序(单源最短路径Bellman-Ford)

    该算法可以用来解决一般(边的权值为负)的单源最短路径问题,而dijkstra只能解决权值非负的情况. 此算法使用松弛技术,对每一个顶点,逐步减少源到该顶点的路径的估计值,直到达到最短的路径. 算法运算 ...

  6. jquery:jqery表单属性 值操作

    重置表单(且清空隐藏域)  $('#myform')[0].reset() ​​ DOM属性相关操作 返回属性值 $(selector).attr(attribute) 设置属性值 $(selecto ...

  7. yjh_study_command

    1.show current user in oralce ansower:show user 2.search  name of table  in current user model. answ ...

  8. 关于main函数的参数,argc,argv的内部机制

    偶尔对main函数的参数感兴趣,写了个程序验证. int main(int argc,char **argv) 首先,解释两点: 第一 .系统将参数列表,即我们在shell下输入的命令,存储到一个一维 ...

  9. Linux --Apache服务搭建

    Apache网站服务 1.基本配置 安装 [root@localhost /]# rpm -e httpd --nodeps --卸载rpm方式安装的httpd [root@localhost qwe ...

  10. delphi7 打开project/options 出错

    出错提示:Access violation at address 0012F88F. Write of address 0012F88F.然后又提示一条:Access violation at add ...