Spring之AOP实现


前言

AOP(Aspect Oriented Programming)称为面向切面编程,在程序开发中主要用来解决一些系统层面上的问题,比如日志,事务,权限等待。


环境搭建

1、pom.xml 配置AOP依赖

        <!-- AOP 依赖-->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.4</version>
        </dependency>

2、Spring配置文件的命名空间中加入aop头文件

<beans xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/aop                                                                      
                            https://www.springframework.org/schema/aop/spring-aop.xsd">

前置业务类编写

1、Admin的service

package com.ex.service;

public interface IAdminService {
    public void saveAdmin(String name);
}
package com.ex.service.impl;

@Service
public class adminServiceImpl implements IAdminService {

    @Override
    public void saveAdmin(String name) {
        System.out.println("save admin method");
    }
}

2、user的service

package com.ex.service;

public interface IUserService {
    public void selectUser(int id);
}
package com.ex.service.impl;

@Service
public class userServiceImpl implements IUserService {

    @Override
    public void selectUser(int id) {
        System.out.println("user select method");
    }
}

一、注解实现AOP

1.编写注解实现的增强类

@Component
@Aspect
public class LogAdvice {
    // springaop自动的5种aop这里全部列出
    // *返回类型,包名,*类名,*方法名,(..)任何参数
    @Before("execution(* com.ex.service.impl.*.*(..))")
    public void before(){
        System.out.println("---------方法执行前before()---------");
    }
    @After("execution(* com.ex.service.impl.*.*(..))")
    public void after(){
        System.out.println("---------方法执行后after()---------");
    }
    @AfterReturning("execution(* com.ex.service.impl.*.*(..))")
    public void afterReturning(){
        System.out.println("---------方法返回后afterReturning()---------");
    }
    @Around("execution(* com.ex.service.impl.*.*(..))")
    public void around(ProceedingJoinPoint jp) throws Throwable {
        System.out.println("-------环绕前-------");
        System.out.println("签名(拿到方法名):"+jp.getSignature());
        //执行目标方法proceed
        Object proceed = jp.proceed();
        System.out.println("-------环绕后------");
        System.out.println(proceed);
    }
    @AfterThrowing("execution(* com.xinzhi.service.impl.*.*(..))")
    public void afterThrow() {
        System.out.println("--------------有异常发生-----------------" + new Date());
    }
}

2.在Spring配置文件中,注册bean,并增加支持注解的配置

    <!-- 扫包:如果使用了注解,需要在开始之前去扫包-->
    <context:component-scan base-package="com.ex"/>
    <!-- aop 注解实现 配置 -->
    <aop:aspectj-autoproxy/>

3.测试

    @Test
    public void testAop(){
        userService.selectUser(1);
        System.out.println("--------------------------------");
        adminService.saveAdmin("aa");
    }

结果

-------环绕前-------
签名(拿到方法名):void com.ex.service.IUserService.selectUser(int)
---------方法执行前before()---------
user select method
-------环绕后------
null
---------方法执行后after()---------
---------方法返回后afterReturning()---------
--------------------------------
-------环绕前-------
签名(拿到方法名):void com.ex.service.IAdminService.saveAdmin(String)
---------方法执行前before()---------
save admin method
-------环绕后------
null
---------方法执行后after()---------
---------方法返回后afterReturning()---------

二、配置文件实现AOP

1.编写自定义增强类

public class MyAOP {
    public void before(){
        System.out.println("---------执行方法前打印日志--------------自定义");
    }
    public void after(){
        System.out.println("---------执行方法后打印日志--------------自定义");
    }
}

2.Spring配置文件中,注册bean,配置增强

    <!--注册bean--> 
    <bean id="myAop" class="com.xinzhi.aop.MyAop"/>
    
    <!--aop的配置-->
    <aop:config>
        <!-- ref 自定义切面类 -->
        <aop:aspect ref="myAOP">
            <!-- 切入点配置 -->
            <aop:pointcut id="pointcut1" expression="execution(* com.ex.service.impl.adminServiceImpl.*(..))"/>
            <aop:pointcut id="pointcut2" expression="execution(* com.ex.service.impl.userServiceImpl.*(..))"/>
            <!-- 织入 -->
            <aop:before pointcut-ref="pointcut1" method="before"/>
            <aop:after pointcut-ref="pointcut2" method="after"/>
        </aop:aspect>
    </aop:config>

2.测试

    @Test
    public void testAop2(){
        userService.selectUser(1);
        System.out.println("--------------------------------");
        adminService.saveAdmin("aa");
    }

结果

user select method
---------执行方法后打印日志--------------自定义
--------------------------------
---------执行方法前打印日志--------------自定义
save admin method

总结

AOP就是对指定的一批的方法在其执行过程中进行一个统一的处理,将大量重复性的工作抽离了出来,省事!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值