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在编码中的应用主要有如下几个方面: 日志记录,跟踪,优化和监控 事务的处理 持久 ...
随机推荐
- Spring @Resource, @Autowired and @Inject 注入
Overview I’ve been asked several times to explain the difference between injecting Spring beans with ...
- easyui datagrid 显示 footer
1.设置 showFooter为true $grid = $dg.datagrid({ // fit: true, //fitColumns: true, // pagination: false, ...
- ArcGIS Engine从服务器(ArcSDE geodatabases)读取数据
从远程服务器读取数据进行处理,直接贴代码: public class ConnectDB { private static String SERVER = "xxx.xxx.xxx.xxx& ...
- zookeeper的JavaAPI
org.apache.zookeeper.Zookeeper Zookeeper 是在 Java 中客户端主类,负责建立与 zookeeper 集群的会话,并提供方法进行操作. org.apache. ...
- SSM整合的简单实现
整合需要的jar包和源码将在文末给出 本文参考黑马程序员视频,由于视频用的环境和我使用的环境不同,建议使用我的环境及jar包(比较新) 一 整合思路 第一步 整合dao层 mybatis和spring ...
- /etc/hosts.allow和/etc/hosts.deny详解
今天遇到一台服务器22端口正常,但是通过ssh连接的问题.排查了防火墙和端口问题,半天没有找出来原因,后来求助大神,终于明白了通过etc目录下hosts.deny和hosts.allow文件可以限制远 ...
- oracle_union_operator
SQL: UNION Operator This SQL tutorial explains how to use the SQL UNION operator with syntax and exa ...
- SpringMvc-自定义视图
1.创建视图: 注意:创建视图的时候需要实现View接口的俩个方法 package com.atguigu.springmvc.views; import java.util.Date; import ...
- strtoul (将字符串转换成无符号长整型数)
strtoul strtoul (将字符串转换成无符号长整型数) 相关函数 atof,atoi,atol,strtod,strtol 表头文件 #include<stdlib.h> 定义函 ...
- Gecko Robotics, Inc. SE II Test OA -- 菜到扣脚
There are three problems in hackrank. two sum http request to get title binary search (find first la ...