springboot异步和切面_在Springboot中采用@AspectJ的注解方式实现AOP(面向切面)编程...

本文介绍了如何在SpringBoot中使用@AspectJ注解实现AOP(面向切面)编程。首先,定义了UserService接口及其实现类,然后创建切面类MyAspect,包括@Pointcut、@Before、@After、@AfterReturning和@AfterThrowing等注解的使用,最后展示了配置类和启动文件的设置。通过实例展示了AOP在方法调用前后的处理流程。
摘要由CSDN通过智能技术生成

因为Spring AOP只能对方法进行拦截,所以首先要确定需要拦截什么方法,让它能织入约定的流程中去。

步骤1:确定连接点(在spring中就是什么类的什么方法)

1.1 创建一个UserService接口,它有userService方法

package com.springboot.aoparound.aspect.service;

import com.springboot.aoparound.pojo.User;

/**

* 描述:

*

* @author

* @create 2019-03-24 18:20

*/

public interface UserService {

public void userService(User user);

}

当然这里需要创建一个普通java对象pojo类,代码如下:

package com.springboot.aoparound.pojo;

import org.springframework.stereotype.Component;

/**

* 描述:

*

* @author

* @create 2019-03-24 18:18

*/

@Component

public class User {

private Long id;

private String userName;

private String note;

public Long getId() {

return id;

}

public void setId(Long 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;

}

}

1.2 给它写一个实现类

package com.springboot.aoparound.aspect.service.userserviceimpl;

import com.springboot.aoparound.aspect.service.UserService;

import com.springboot.aoparound.pojo.User;

import org.springframework.stereotype.Service;

/**

* 描述:

*

* @author

* @create 2019-03-24 18:24

*/

@Service

public class UserServiceImpl implements UserService {

@Override

public void userService(User user) {

if(user == null){

throw new RuntimeException("输入参数异常...");

}

System.out.println("\tid="+user.getId());

System.out.println("\tuserName="+user.getUserName());

System.out.println("\tnote="+user.getNote());

}

}

这样一个普通的服务接口和实现类就实现了,接下来就是以userService方法为连接点,进行AOP编程。

步骤2:有了连接点,我们还需要一个切面,,通过它可以描述AOP的其它信息,用来描述流程的织入。创建切面类如下:

package com.springboot.aoparound.aspect;

import com.springboot.aoparound.aspect.validator.UserValidator;

import com.springboot.aoparound.aspect.validator.impl.UserValidatorImpl;

import org.aspectj.lang.ProceedingJoinPoint;

import org.aspectj.lang.annotation.*;

/**

* 描述:

*

* @author

* @create 2019-03-24 18:29

*/

@Aspect

public class MyAspect {

@Pointcut("execution(* com.springboot.aoparound.aspect.service.userserviceimpl.UserServiceImpl.userService(..))")

public void pointCut(){

}

@Before("pointCut()")

public void before(){

System.out.println("this is before...");

}

@After("pointCut()")

public void after(){

System.out.println("this is after...");

}

@AfterReturning("pointCut()")

public void afterReturning(){

System.out.println("this is afterReturning...");

}

@AfterThrowing("pointCut()")

public void afterThrowing(){

System.out.println("this is afterThrowing...");

}

}

如果对于这个切面类不能理解,我上一篇笔记是解释什么是约定编程的,可以帮助理解切面编程的。

步骤三:创建一个Config类作为java配置文件类,它是一个控制器,通过它,我们能在web浏览器的搜索栏输入URL和对应的参数,然后通过自动注入的UserService接口就能够进行用户信息的打印,因为方法标注了@ResponseBody,所以Spring MVC会将其转换成JSON响应请求。

package com.springboot.aoparound.config;

import com.springboot.aoparound.aspect.service.UserService;

import com.springboot.aoparound.aspect.validator.UserValidator;

import com.springboot.aoparound.pojo.User;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;

/**

* 描述:

*

* @author

* @create 2019-03-24 18:41

*/

@Controller

@RequestMapping("/user")

public class Config {

@Autowired

private UserService userService = null;

@RequestMapping("/print")

@ResponseBody

public User getUser(Long id,String userName,String note){

User user = new User();

user.setId(id);

user.setUserName(userName);

user.setNote(note);

// user = null;

userService.userService(user);

return user;

}

}

步骤四:配置启动文件

package com.springboot.aoparound.main;

import com.springboot.aoparound.aspect.MyAspect;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.context.annotation.Bean;

@SpringBootApplication(scanBasePackages = "com.springboot.aoparound")

public class AoparoundApplication {

@Bean("myAspect")

public MyAspect initMyAspect(){

return new MyAspect();

}

public static void main(String[] args) {

SpringApplication.run(AoparoundApplication.class, args);

}

}

然后通过如下的路径启动项目:

看到后台运行如下:

运行成功后再web浏览器的搜索框输入:http://localhost:8080/user/print?id=1&userName=shuxianliang&note=biji

会看到浏览器返回了参数如下图:

后台打出的日志如下图所示:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值