Spring boot with Aop(Aspect)

Spring boot with Aop(Aspect)

节选自《Netkiller Spring Cloud 手札》

Maven

 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-aop</artifactId>
 </dependency> 

Pojo 类

package cn.netkiller.aop.pojo;

import lombok.Data;

@Data
public class Employee {
 private String id;
 private String name;

 public Employee() {
 // TODO Auto-generated constructor stub
 }

}

Service 类

package cn.netkiller.aop.service;

import org.springframework.stereotype.Service;

import cn.netkiller.aop.pojo.Employee;

@Service
public class EmployeeService {

 public EmployeeService() {
 // TODO Auto-generated constructor stub
 }

 public Employee createEmployee(String id, String name) {

 Employee emp = new Employee();
 emp.setName(name);
 emp.setId(id);
 return emp;
 }

 public void deleteEmployee(String id) {

 }
}

Aspect 类

package cn.netkiller.aop.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class EmployeeServiceAspect {
 public EmployeeServiceAspect() {
 }

 @Before(value = "execution(* cn.netkiller.aop.service.EmployeeService.*(..)) and args(id, name)")
 public void beforeAdvice(JoinPoint joinPoint, String id, String name) {
 System.out.println("Before method:" + joinPoint.getSignature());

 System.out.println("Creating Employee with id: " + id + ", name: " + name);
 }

 @After(value = "execution(* cn.netkiller.aop.service.EmployeeService.*(..)) and args(id,name)")
 public void afterAdvice(JoinPoint joinPoint, String id, String name) {
 System.out.println("After method:" + joinPoint.getSignature());

 System.out.println("Successfully created Employee with id: " + id + ", name: " + name);
 }
}  

控制器

package cn.netkiller.aop.controller;

import org.springframework.beans.factory.annotation.Autowired;
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 cn.netkiller.aop.pojo.Employee;
import cn.netkiller.aop.service.EmployeeService;

@RestController
public class EmployeeController {

 public EmployeeController() {
 // TODO Auto-generated constructor stub
 }

 @Autowired
 private EmployeeService employeeService;

 @RequestMapping(value = "/add/employee", method = RequestMethod.GET)
 public Employee addEmployee(@RequestParam("id") String id, @RequestParam("name") String name) {

 return employeeService.createEmployee(id, name);

 }

 @RequestMapping(value = "/remove/employee", method = RequestMethod.GET)
 public String removeEmployee(@RequestParam("id") String id) {

 employeeService.deleteEmployee(id);

 return "Employee removed";
 }

}

Application

package cn.netkiller.aop;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
 public static void main(String[] args) {
 System.out.println("Hello World!");
 SpringApplication.run(Application.class, args);
 }
}

测试触发 Aspect

neo@MacBook-Pro ~ % curl http://localhost:8080/add/employee\?id\=1\&name\=neo
{"id":"1","name":"neo"} 

控制台输出效果

Before method:Employee cn.netkiller.aop.service.EmployeeService.createEmployee(String,String)
Creating Employee with id: 1, name: neo
After method:Employee cn.netkiller.aop.service.EmployeeService.createEmployee(String,String)
Successfully created Employee with id: 1, name: neo
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

netkiller-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值