Spring Aop使用demo(Spring Boot)

版权声明:版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37598011/article/details/84257414

pom.xml:

	<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-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

创建User类:

package com.example.demo.dao;
/* 
* @author zzf 
* @date 2018年11月19日 下午5:19:28 
*/
public class User {

	String name;
	Integer age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public User(String name, Integer age) {
		super();
		this.name = name;
		this.age = age;
	}
	@Override
	public String toString() {
		return "User [name=" + name + ", age=" + age + "]";
	}
	
}

创建接口:

package com.example.demo.concert;

import com.example.demo.dao.User;

/* 
* @author zzf 
* @date 2018年11月19日 上午11:09:02 
*/
public interface Performance {

	public void perform1();
	
	public void perform2() throws Exception;
	
	public void perform3(int age,String name);
	
	public void perform4(User user);
}

实现类:

package com.example.demo.concert.impl;

import org.springframework.stereotype.Component;

import com.example.demo.concert.Performance;
import com.example.demo.dao.User;

/* 
* @author zzf 
* @date 2018年11月19日 下午1:33:49 
*/
@Component
public class PerformanceImpl implements Performance{

	@Override
	public void perform1() {
		// TODO Auto-generated method stub
		System.out.println("方法运行1");
	}

	@Override
	public void perform2() throws Exception {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		System.out.println("方法运行2");
		throw new Exception("抛出异常");
	}

	@Override
	public void perform3(int age,String name) {
		// TODO Auto-generated method stub
		System.out.println("方法运行-age:"+age+"-name:"+name);
	}

	@Override
	public void perform4(User user) {
		// TODO Auto-generated method stub
		System.out.println(user);
	}


}

切面类:

package com.example.demo;

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;

import com.example.demo.dao.User;

/* 
* @author zzf 
* @date 2018年11月19日 下午1:14:47 
*/
@Aspect
@Component
public class Audience {
	
	
	//定义切点
	@Pointcut("execution(** com.example.demo.concert.Performance.*(..))")//所有的方法任意参数
	public void performance() {}
	//传递单个值
	@Pointcut("execution(** com.example.demo.concert.Performance.*(int,String)) && args(age,name)")
	public void performance2(int age,String name) {}
	//传递对象
	@Pointcut("execution(** com.example.demo.concert.Performance.*(com.example.demo.dao.User)) && args(user)")
	public void performance3(User user) {}
	
	@Before("performance()")
	public void beforePerformance1() {
		System.out.println("performance-方法开始前1");
	}
	
	@Before("performance()")
	public void beforePerformance2() {
		System.out.println("performance-方法开始前2");
	}
	
	@AfterReturning("performance()")
	public void AfterPerformance() {
		System.out.println("performance-方法正常退出后");
	}
	
	@AfterThrowing("performance()")
	public void AfterTrownPerformance() {
		System.out.println("performance-方法异常退出");
	}
	
	@Around("performance()")
	public void AroudPerformance(ProceedingJoinPoint jp) throws Throwable {
		System.out.println("performance-环绕通知方法前");
		jp.proceed();
		System.out.println("performance-环绕通知方法后");
	}
	/***************************************************************/
	@Before("performance2(age,name)")
	public void beforePerformance1_2(int age,String name) {
		System.out.println("performance2-方法开始前1-age:"+age+"-name:"+name);
	}
	
	@Before("performance2(age,name)")
	public void beforePerformance2_2(int age,String name) {
		System.out.println("performance2-方法开始前2-age:"+age+"-name:"+name);
	}
	
	@AfterReturning("performance2(age,name)")
	public void AfterPerformance2(int age,String name) {
		System.out.println("performance2-方法正常退出后-age:"+age+"-name:"+name);
	}
	
	@AfterThrowing("performance2(age,name)")
	public void AfterTrownPerformance2(int age,String name) {
		System.out.println("performance2-方法异常退出-age:"+age+"-name:"+name);
	}
	
	@Around("performance2(age,name)")
	public void AroudPerformance2(ProceedingJoinPoint jp,int age,String name) throws Throwable {
		System.out.println("performance2-环绕通知方法前-age:"+age+"-name:"+name);
		jp.proceed();
		System.out.println("performance2-环绕通知方法后-age:"+age+"-name:"+name);
	}
	
	/**********************************************************************/
	@Around("performance3(user)")
	public void AroudPerformance3(ProceedingJoinPoint jp,User user) throws Throwable {
		System.out.println("performance3-环绕通知方法前-User:"+user);
		jp.proceed();
		System.out.println("performance3-环绕通知方法后-User:"+user);
	}
}

控制器:

package com.example.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.concert.Performance;
import com.example.demo.dao.User;
import com.fasterxml.jackson.databind.ser.std.StdKeySerializers.Default;

/* 
* @author zzf 
* @date 2018年11月19日 下午1:38:58 
*/
@RestController
public class TestController {

	@Autowired
	Performance performance;
	
	@RequestMapping(value = "/test1")
	public void run1() throws Exception {
		performance.perform1();
	}
	
	@RequestMapping(value = "/test2")
	public void run2() throws Exception {
		performance.perform2();
	}
	
	@RequestMapping(value = "/test3")
	public void run3(int age,String name) throws Exception {
		performance.perform3(age,name);
	}
	
	@RequestMapping(value = "/test4")
	public void run4(User a) throws Exception {
		performance.perform4(a);
	}
}

目录结构:

运行:

http://localhost:8888/test4?age=9&name=aaa

http://localhost:8888/test3?age=99&name=嗷嗷嗷

http://localhost:8888/test2

http://localhost:8888/test1

猜你喜欢

转载自blog.csdn.net/qq_37598011/article/details/84257414
今日推荐