Home | 簡體中文 | 繁體中文 | 雜文 | 知乎專欄 | Github | OSChina 博客 | 雲社區 | 雲棲社區 | Facebook | Linkedin | 視頻教程 | 打賞(Donations) | About
知乎專欄多維度架構 | 微信號 netkiller-ebook | QQ群:128659835 請註明“讀者”

2.43. Spring boot with Aop

2.43.1. Aspect

2.43.1.1. Maven

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

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

}
			
			
			

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

	}
}
			
			
			

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

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

}
			
			
			

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

2.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