Spring面向切面编程

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

Spring学习记录
03. Spring面向切面编程


提示:以下是本篇文章正文内容,下面案例可供参考

AOP:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。

使用Spring实现AOP

重点】使用AOP织入,需要导入一个依赖包!

<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.8</version>
</dependency>

第一种方式:使用Spring API实现

1.创建业务接口和实现类:

public interface UserService {
    public void add();
    public void delete();
    public void update();
    public void select();
}
@Component("userService")
public class UserServiceImpl implements UserService {
    public void add() {
        System.out.println("增");
    }

    public void delete() {
        System.out.println("删");
    }

    public void update() {
        System.out.println("改");
    }

    public void select() {
        System.out.println("查");
    }

}

2.创建一个增强类,包含前置增强和后置增强

@Component("myLog")
public class MyLog implements MethodBeforeAdvice, AfterReturningAdvice {
    /**
     * 前置增强
     *
     * @param method 要执行的目标对象的方法
     * @param args   参数
     * @param target 目标对象
     * @throws Throwable
     */
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println(target.getClass().getName() + "的" + method.getName() + "被执行");
    }

    /**
     * 后置增强
     *
     * @param returnValue   返回值
     * @param method    被调用的方法
     * @param args  被调用方法的对象的参数
     * @param target    被调用得目标对象
     * @throws Throwable
     */
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println("执行了:"+method.getName()+",返回结果为:"+returnValue);
    }
}

3.在Spring文件中注册,并实现aop切入实现,注意约束导入(applicationContent.xml文件中)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
    <context:annotation-config/>
    <!--定义包下的所有类为bean-->
    <context:component-scan base-package="com.fridas"/>

    <!--方式一:使用原生Spring API接口-->
    <!--aop的配置:需要导入aop的约束-->
    <aop:config>
        <!--pointcut:切入点  expression:表达式匹配要执行的方法-->
        <aop:pointcut id="pointcut" expression="execution(* com.fridas.service.UserServiceImpl.*(..))"/>
        <!--执行环绕; advice-ref执行方法 . pointcut-ref切入点-->
        <aop:advisor advice-ref="myLog" pointcut-ref="pointcut"/>
    </aop:config>

</beans>

4.测试类:

public class MyTest {
    @Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = context.getBean("userService", UserService.class);
        userService.add();
    }
}

第二种方式:使用自定义类实现

目标业务类不变依旧是userServiceImpl

1.创建一个自己的切入类

@Component("diy")
public class DiyPointCut {
    public void Before(){
        System.out.println("***方法输出之前***");
    }
    public void After(){
        System.out.println("***方法输出之后***");
    }
}

2.在Spring文件中配置(注册bean已由定义包下的所有类为bean完成)

<context:component-scan base-package="com.fridas"/>
    <!--方式二:使用自定义切入类-->
    <aop:config>
        <aop:aspect ref="diy">
            <!--切入点-->
            <aop:pointcut id="point" expression="execution(* com.fridas.service.UserServiceImpl.*(..))"/>
            <!--通知-->
            <aop:before method="Before" pointcut-ref="point"/>
            <aop:after method="After" pointcut-ref="point"/>
        </aop:aspect>
    </aop:config>

3.测试类不变

第三种方式:使用注解实现

1.创建一个注解实现增强类

@Component
@Aspect
public class AnnotationPointCut {
    @Before("execution(* com.fridas.service.UserServiceImpl.*(..))")
    public void before() {
        System.out.println("**使用方法之前**");
    }

    @After("execution(* com.fridas.service.UserServiceImpl.*(..))")
    public void after() {
        System.out.println("**使用方法之后**");
    }

    /**
     * 环绕增强中,可以给定一个参数,代表要获取处理切入的点
     * @param jp    切入点
     * @throws Throwable
     */
    @Around("execution(* com.fridas.service.UserServiceImpl.*(..))")
    public void around(ProceedingJoinPoint jp) throws Throwable {
        System.out.println("环绕前");
        System.out.println("签名:" + jp.getSignature());
        Object proceed = jp.proceed();
        System.out.println("环绕后");
        System.out.println(proceed);
    }
}

2.在Spring配置文件中,注册bean,并增加支持注解的配置
(注册bean已由定义包下的所有类为bean完成)

<context:component-scan base-package="com.fridas"/>
    <!--方式三:使用注解实现aop-->
    <!--定义bean,已由定义包下的所有类完成,在需要定义的类前加注解@component-->
    <!--开启注解-->
    <aop:aspectj-autoproxy/>

总结

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值