Spring Aop使用demo(Spring Boot)
阿新 • • 發佈:2018-11-21
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