Home | 简体中文 | 繁体中文 | 杂文 | 知乎专栏 | Github | OSChina 博客 | 云社区 | 云栖社区 | Facebook | Linkedin | 视频教程 | 打赏(Donations) | About
知乎专栏多维度架构

5.43. Spring boot with Aop

5.43.1. Aspect

5.43.1.1. Maven

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

5.43.1.2. 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
	}

}
			
			
			

5.43.1.3. 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) {

	}
}
			
			
			

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

5.43.1.5. 控制器

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

}
			
			
			

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

5.43.1.7. 测试

触发 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