SpringBoot第 9 讲:SpringBoot+AOP

一、创建Maven项目

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

SpringBoot第 9 讲:SpringBoot+AOP_第1张图片 

 二、修改pom.xml

添加web支持


	4.0.0
	
	
		org.springframework.boot
		spring-boot-starter-parent
		1.5.2.RELEASE
	
	

	org.personal.qin.demos
	aop_demo
	1.0.0-SNAPSHOT
	jar

	
		UTF-8
		UTF-8
		
		1.8
	

	
		
		
			org.springframework.boot
			spring-boot-starter-web
		
		
		
		
		
			org.springframework.boot
			spring-boot-starter-aop
		
		

		
		
			org.springframework
			spring-webmvc
		
		

	

	
		${project.artifactId}
		
			
			
				org.apache.maven.plugins
				maven-resources-plugin
				
					UTF-8
				
			
			
			
				org.apache.maven.plugins
				maven-compiler-plugin
				
					1.8
					1.8
					UTF-8
				
			
			
			
				org.springframework.boot
				spring-boot-maven-plugin
			
			
		
	

三、自定义切面

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);
	}
}

七、源代码

https://download.csdn.net/download/qzc70919700/30545933

你可能感兴趣的:(SpringBoot+,maven,spring,boot,java)