以下内容是模仿杨开振<<深入浅出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. POJ 3667 线段树区间合并

    http://www.cnblogs.com/scau20110726/archive/2013/05/07/3065418.html 用线段树,首先要定义好线段树的节点信息,一般看到一个问题,很难很 ...

  2. Canvas绘制图片模糊

    canvas是html5的新标签,是个可以绘制图形的画布,画布的默认大小为300x150.在自定义绘制画布大小的时候有注意的问题,就是用样式来设置高度和宽度的时候 比如 <div style=& ...

  3. vue1.0 与 Vue2.0的一些区别 及用法

    1.Vue2.0的模板标记外必须使用元素包起来: eg:Vue1.0的写法 <!DOCTYPE html> <html> <head> <meta chars ...

  4. CSS3控制单行文本的溢出

    text-overflow用来设置是否使用一个省略标记(...)标示对象内文本的溢出.语法: 但是text-overflow只是用来说明文字溢出时用什么方式显示,要实现溢出时产生省略号的效果,还须定义 ...

  5. CNN 和RNN 中input 长度不一致问题

    转自:https://www.jianshu.com/p/86d667ee3c62,感谢分享! pad_sequences & Masking layer 上面提到,文本数据也可以用CNN来处 ...

  6. 再学UML-Bug管理系统UML2.0建模实例(一)

    1.项目概述       随着软件项目规模和复杂性的增大,有效跟踪和管理项目中存在的缺陷Bug变得越来越重要.每一个软件企业都需要妥善处理软件中的缺陷,这将直接关系到软件过程质量与软件产品质量,但并非 ...

  7. March 20 2017 Week 12 Monday

    A goal is a dream with a deadline. 目标就是给梦想一个期限. Dream without dealine is just daydream, because you ...

  8. 阿里云上到底能运行SAP哪些产品?

    本文主要内容大部分来源于SAP已经发布的note:  2552731 - SAP Applications on Alibaba Cloud: Supported Products and IaaS ...

  9. vue.js--基础 数据的双向绑定

    所谓双向绑定:就是改变modle,就会改变view,改变view,也会改变modle 下面案例,点击getMthod(),获取msg的内容,在点击setMthod()改变msg的内容,你会发现H1的值 ...

  10. Codeforces Round #432 (Div. 2)

    A. Arpa and a research in Mexican wave Arpa is researching the Mexican wave. There are n spectators ...