SpringBoot第 9 讲:SpringBoot+AOP

74 阅读2分钟

一、创建Maven项目

参考:SpringBoot第 1 讲:HelloWorld_秦毅翔的专栏-CSDN博客

二、修改pom.xml

添加web支持

<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">
	<modelVersion>4.0.0</modelVersion>
	<!-- SpringBoot支持01、parent:Begin -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.2.RELEASE</version>
	</parent>
	<!-- SpringBoot支持01、parent:End -->
 
	<groupId>org.personal.qin.demos</groupId>
	<artifactId>aop_demo</artifactId>
	<version>1.0.0-SNAPSHOT</version>
	<packaging>jar</packaging>
 
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<!-- java版本 -->
		<java.version>1.8</java.version>
	</properties>
 
	<dependencies>
		<!-- SpringBoot:Begin -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- SpringBoot:End -->
		
		<!-- aop:Begin -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-aop</artifactId>
		</dependency>
		<!-- aop:End -->
 
		<!-- SpringMVC:Begin -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
		</dependency>
		<!-- SpringMVC:End -->
 
	</dependencies>
 
	<build>
		<finalName>${project.artifactId}</finalName>
		<plugins>
			<!-- 资源文件拷贝插件 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-resources-plugin</artifactId>
				<configuration>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>
			<!-- java编译插件 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>
			<!-- SpringBoot支持03、添加SpringBoot的插件支持:Begin -->
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
			<!-- SpringBoot支持03、添加SpringBoot的插件支持:End -->
		</plugins>
	</build>
</project>

三、自定义切面

Spring面向切面编程,主要是用于操作Service层,对应用添加功能,将Service切成多个切面,分别是调用方法前、执行方法后等,实现诸多功能,比如数据库读写分离,或者日志记录等

package demo.aop.aspect;
 
import java.util.Arrays;
 
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;
import org.springframework.context.annotation.Configuration;
 
@Aspect
@Configuration
public class TestAspect {
	// 抽取公共的切入点表达式
	// 1、本类引用
	// 2、其他的切面引用
	@Pointcut("execution(* demo.aop.service..*.insert*(..)) "
			+ "|| execution(* demo.aop.service..*.add*(..)) "
			+ "|| execution(* demo.aop.service..*.update*(..)) "
			+ "|| execution(* demo.aop.service..*.edit*(..)) "
			+ "|| execution(* demo.aop.service..*.delete*(..)) "
			+ "|| execution(* demo.aop.service..*.remove*(..))")
	public void pointCut() {
	}
 
	// @Before在目标方法之前切入;切入点表达式(指定在哪个方法切入)
	@Before("pointCut()")
	public void logStart(JoinPoint joinPoint) {
		Object[] args = joinPoint.getArgs();
		System.out
				.println("" + joinPoint.getSignature().getName() + "运行。。。@Before:参数列表是:{" + Arrays.asList(args) + "}");
	}
 
	@After("demo.aop.aspect.TestAspect.pointCut()")
	public void logEnd(JoinPoint joinPoint) {
		System.out.println("" + joinPoint.getSignature().getName() + "结束。。。@After");
	}
 
	// JoinPoint一定要出现在参数表的第一位
	@AfterReturning(value = "pointCut()", returning = "result")
	public void logReturn(JoinPoint joinPoint, Object result) {
		System.out.println("" + joinPoint.getSignature().getName() + "正常返回。。。@AfterReturning:运行结果:{" + result + "}");
	}
 
	@AfterThrowing(value = "pointCut()", throwing = "exception")
	public void logException(JoinPoint joinPoint, Exception exception) {
		System.out.println("" + joinPoint.getSignature().getName() + "异常。。。异常信息:{" + exception + "}");
	}
 
}

四、定义Service层

package demo.aop.service.impl;
 
import org.springframework.stereotype.Service;
 
import demo.aop.service.TestService;
import demo.aop.utils.Log;
 
@Service
public class TestServiceImpl implements TestService {
 
	@Override
	public int insert(int id, String name) {
		Log.i(getClass(), "执行Service层的inser方法,需要AOP进行管理");
		return 1;
	}
 
	@Override
	public String getName(int id) {
		Log.i(getClass(), "执行Service层的select方法,不需要进AOP管理");
		return "admin";
	}
 
}

五、定义RestController接收Http请求

package demo.aop.controller;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import demo.aop.service.TestService;
 
@RestController
@RequestMapping("aop")
public class TestController {
	
	@Autowired
	private TestService service;
	
	@GetMapping("t1")
	public String test1() {
		service.insert(100, "admin");
		return "在控制台查看,需要AOP管理";
	}
	
	@GetMapping("t2")
	public String test2() {
		service.getName(100);
		return "在控制台查看,不需要AOP管理";
	}
}

六、启动BootApplication

package demo.aop;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@SpringBootApplication
public class AopBootApplication {
 
	public static void main(String[] args) {
		SpringApplication.run(AopBootApplication.class, args);
	}
}