Spring boot——AOP、@Aspect例子

本文详细介绍了如何在SpringBoot项目中使用AOP(面向切面编程),通过具体代码示例展示了如何创建Controller、Service接口及其实现类,并利用@Aspect注解定义切面类,实现对UserService中printUser方法的前置、后置及后置返回通知。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码例子
在这里插入图片描述
Controller

package com.springboot.chapter4.aspect.controller;

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.springboot.chapter4.aspect.service.UserService;
import com.springboot.chapter4.pojo.User;

@Controller
@RequestMapping("/user")
public class UserController {
	
	@Autowired
	@Qualifier("userServiceimpl")//避免歧义
	private UserService userService = null;
	
	@RequestMapping("/print")
	@ResponseBody
	public User printUser(Integer id) {
		System.out.println("User");
		User user = new User();
		user.setId(id);
		user.setUsername("wang.li");
		user.setNote("AAA");
		userService.printUser(user);
		return user;
	}

}

UserService

package com.springboot.chapter4.aspect.service;
import com.springboot.chapter4.pojo.User;

public interface UserService {
	public void printUser(User user);
}

UserServiceimpl

package com.springboot.chapter4.aspect.service.impl;

import org.springframework.stereotype.Service;
import com.springboot.chapter4.aspect.service.UserService;
import com.springboot.chapter4.pojo.User;
/**
 * @Service 是标记实现类上的。
  *   因为@Service是把spring容器中的bean进行实例化,也就是等同于new操作,
  *   只有实现类是可以进行new实例化的,而接口则不能,所以是加在实现类上的。不信你标注到接口上,会报BeanCreationException的。
 * @author XXX
 *
 */
@Service
public class UserServiceimpl implements UserService{
	@Override
	public void printUser(User user) {
		// TODO Auto-generated method stub
		if(user == null) {
			throw new RuntimeException("检查用户参数是否为空. .. ...");
		}
		System.out.println("id = "+user.getId());
		System.out.println("username = "+user.getUsername());
		System.out.println("note = "+user.getNote());
	}
}

AnimalServiceimpl

package com.springboot.chapter4.aspect.service.impl;

import org.springframework.stereotype.Service;
import com.springboot.chapter4.aspect.service.UserService;
import com.springboot.chapter4.pojo.User;

@Service
public class AnimalServiceimpl implements UserService{
	@Override
	public void printUser(User user) {
		// TODO Auto-generated method stub
		System.out.println("Animal");
	}
}

User

package com.springboot.chapter4.pojo;

public class User {
	private Integer id;
	private String username;
	private String note;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getNote() {
		return note;
	}
	public void setNote(String note) {
		this.note = note;
	}
}

aspect

package com.springboot.chapter4.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

import com.springboot.chapter4.pojo.User;
/**
 *	使用@Aspect注解将一个java类定义为切面类
 */
@Aspect
public class MyAspect {
	/**
	 * execution:匹配连接点    
	 * *: 任意返回类型   
	 * com.springboot.chapter4.aspect.service.impl.UserServiceimpl.printUser:目标对象权限定名 
	 * printUser:目标对象方法        (..):任意参数进行匹配      
	 * 
	 */
	@Pointcut("execution(* com.springboot.chapter4.aspect.service.impl.UserServiceimpl.printUser(..))")
	public void pointCut() {
		
	}
	@Before("pointCut()")//前置通知
	public void befor() {
		System.out.println("befor--------------------");
	}	
	@After("pointCut()")//后置通知
	public void After() {
		System.out.println("after--------------------");
	}
	@AfterReturning("pointCut()")//后置返回通知
	public void AfterReturning(JoinPoint point) { //JointPoint.getArgs()可以获取执行目标方法时传入的参数
		Object[] args = point.getArgs();
		User d = (User) args[0];
		System.out.println("AfterReturning--------------------");
		System.out.println(d.getId());
	}
	/*
	@Around("pointCut()")//环绕通知
	public void Around(ProceedingJoinPoint JoinPoint) throws Throwable {
		System.out.println("Start-Around--------------------");
		JoinPoint.proceed();
		System.out.println("End-Around--------------------");
	}
	 */
}

Application

package com.springboot.chapter4;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.Bean;

import com.springboot.chapter4.aspect.MyAspect;

/**
 * http://localhost:8080/user/print?id=1
 * @author XXX
 *
 */
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class Application {
	@Bean(name="myaspect")
	public MyAspect initMyaspect() {
		return new MyAspect();
	}

	public static void main(String args[]) {
		SpringApplication.run(Application.class, args);
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值