AOP(Aspect Oriented Programming,面向切面编程)是一种编程范式,它旨在将横切关注点(cross-cutting concerns)从应用程序的业务逻辑中分离出来。横切关注点是那些在多个模块中重复出现的功能,如日志记录、性能监控、事务管理、安全控制等。AOP允许开发者将这些关注点模块化,并在不影响应用程序主要功能的情况下,将它们编织到应用程序的各个点上。
@Aspect
、@Before
、@After
、@Around
等)来定义切面和切入点,然后在运行时通过动态代理实现切面的编织。采用@aspect注解来实现aop
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springboot-demo</artifactId>
<groupId>com.et</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>aspect</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
</dependencies>
</project>
package com.et.aspect.controller;
import com.et.aspect.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
public class HelloWorldController {
@Autowired
TestService testService;
@RequestMapping("/hello")
public Map<String, Object> showHelloWorld(){
Map<String, Object> map = new HashMap<>();
String str= testService.sayHello("liming");
map.put("msg",str);
return map;
}
}
package com.et.aspect.service;
public interface TestService {
public String sayHello(String name) ;
}
package com.et.aspect.service.impl;
import com.et.aspect.service.TestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
/**
* @author liuhaihua
* @version 1.0
* @ClassName TestServiceImpl
* @Description todo
* @date 2024/09/05/ 9:21
*/
@Service
@Slf4j
public class TestServiceImpl implements TestService {
@Override
public String sayHello(String name) {
String sss="hello,"+name;
log.info(sss);
return sss;
}
}
package com.et.aspect.aspect;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Aspect
@Component
@Slf4j
public class TaskAspect {
@Pointcut("execution(* com.et.aspect.service..*.*(..))")
public void aopPoint() {
}
@Before("aopPoint()")
public void doBefore(JoinPoint point) throws Throwable {
log.info("before ....");
}
@After("aopPoint()")
public void doAfter(JoinPoint point) throws Throwable {
log.info("after ....");
}
}
2024-09-05 21:52:12.926 INFO 51009 --- [nio-8088-exec-1] com.et.aspect.aspect.TaskAspect : before ....
2024-09-05 21:52:12.939 INFO 51009 --- [nio-8088-exec-1] c.e.aspect.service.impl.TestServiceImpl : hello,liming
2024-09-05 21:52:12.939 INFO 51009 --- [nio-8088-exec-1] com.et.aspect.aspect.TaskAspect : after ....