springmvc中@before_在Springboot中采用@AspectJ的注解方式实现AOP(面向切面)编程

32bc597863e85c924137d117693ae33a.png

因为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 */@Componentpublic 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 */@Servicepublic class UserServiceImpl implements UserService { @Override public void userService(User user) { if(user == null){ throw new RuntimeException("输入参数异常..."); } System.out.println("id="+user.getId()); System.out.println("userName="+user.getUserName()); System.out.println("note="+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 */@Aspectpublic 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); }}

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

b087b79be68cab13da6a9a2415e8ac24.png

看到后台运行如下:

a178fd6b9a7e7dbf7e49b45b6703df05.png

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

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

ed009eeaa993c9117b1c25605bb7fe12.png

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

457b4d820b96c092f3922fface3ed8c1.png

感谢你的阅读,如果想了解更多也可查阅文章下方的其它智能检索文章,相信会有适合你的文章哒,点个赞呗~~~啊哈哈

5676c9965f1983c85e524fa0cefda724.png
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值