项目准备工作
电子货币交易平台:
驾考平台:
教务系统:
Spring AOP中的切面
工程目录:
工程所需的依赖:
pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.newer</groupId>
<artifactId>aop2</artifactId>
<version>0.1</version>
<name>aop2</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
其中AOP所需的依赖:
扫描二维码关注公众号,回复:
11142621 查看本文章
<!-- AOP -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
application.properties(配置信息)
spring.http.log-request-details=true
logging.level.web=debug
Aop2Application.java
package com.newer.aop;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Aop2Application {
public static void main(String[] args) {
SpringApplication.run(Aop2Application.class, args);
}
}
AppService.java
package com.newer.aop;
/**
* 业务逻辑
*
* @author Admin
*
*/
public interface AppService {
String sayHello(String name);
String sayBye(String name);
}
Log.java
package com.newer.aop.aspect;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
//目标
//有效期(保留策略)
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Log {
/**
* 是否启用
* @return
*/
boolean enable() default true;
}
LogAspect.java
package com.newer.aop.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
//组件,容器管理生命周期
@Component
//定义为切面(动态织入业务逻辑代码)
@Aspect
public class LogAspect {
// 切入点,为注解Log标记的位置
// 不同包com.newer.aop.aspect。Log
// @Pointcut("@annotation(com.newer.aop.aspect。Log)")
@Pointcut("@annotation(Log)")
public void pointcut() {
}
// 基于方法的织入
// 5个在方法中插入你的代码的时间点,根据需要定义某几个
//
// @Before
// @After
// @Around
// @AfterThrowing
// @AfterReturning
@Before("pointcut()")
public void a() {
System.out.println("@Before");
}
@After("pointcut()")
public void b() {
System.out.println("@After");
}
// 环绕
// 类似于过滤器
// ProceedingJoinPoint:连接点
@Around("pointcut()")
public Object c(ProceedingJoinPoint joinPoint) throws Throwable {
Object[] args= joinPoint.getArgs();
String arg0=args[0].toString();
if(arg0.length()<4) {
arg0="spring boot";
}else {
arg0=arg0.toUpperCase();
}
args[0]=arg0;
System.out.println("前");
// 执行方法
Object o= joinPoint.proceed(args);
System.out.println("后");
//
return o;
}
@AfterThrowing("pointcut()")
public void d() {
System.out.println("@AfterThrowing");
}
@AfterReturning("pointcut()")
public void e() {
System.out.println("@AfterReturning");
}
}
AppServiceImpl.java
package com.newer.aop;
import org.springframework.stereotype.Service;
import com.newer.aop.aspect.Log;
@Service
public class AppServiceImpl implements AppService{
@Log
@Override
public String sayHello(String name) {
System.out.println("sayHello");
return "hello"+ name;
}
// 基于动态代理模式
// @Log(enable=false)
@Log
@Override
public String sayBye(String name) {
System.out.println("sayBye");
return "bye"+ name;
}
}
AuthRole.java
package com.newer.aop.aspect;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface AuthRole {
Role value();
}
AuthRoleAspect.java
package com.newer.aop.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class AuthRoleAspect {
@Pointcut("@annotation(AuthRole)")
public void pointcut() {
}
/**
*
* @param joinPoint
* @return
* @throws Throwable
*/
@Around("pointcut()")
public Object todo(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("todo");
// 加入特定代码
Object[] args=joinPoint.getArgs();
String from =args[0].toString();
// 验证余额
// 返回null,抛出异常
Object o= joinPoint.proceed(args);
return o;
}
}
Role.java
package com.newer.aop.aspect;
public enum Role {
STUDENT,TEACHER,ADMIN
}
HomeController.java
package com.newer.aop;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import com.newer.aop.aspect.AuthRole;
import com.newer.aop.aspect.Role;
@RestController
public class HomeController {
@Autowired
AppService appService;
/**
* Get /hello/{name
* @param name
* @return
*/
// 访问必须是管理员
@AuthRole(value = Role.ADMIN)
@GetMapping("/hello/{name}")
public String a(@PathVariable String name) {
return appService.sayHello(name);
}
/**
* Get /bye/{name}
* @param name
* @return
*/
// 访问必须是学生
@AuthRole(Role.STUDENT)
@GetMapping("/bye/{name}")
public String b(@PathVariable String name) {
return appService.sayBye(name);
}
}
运行程序,当name的字数少于4时。出现
当name的字数大于4时:
同时控制台出现:
以上就是Spring AOP中的切面以及部分项目阶段的准备工作,有问题的小伙伴,欢迎留言!!!