spring 面向切面编程之自定义切面类

本文介绍了一种使用Spring AOP实现自定义切面的方法,重点讲解了如何通过XML配置文件定义切入点和通知,以及如何在UserServiceImpl类的方法调用前后插入日志记录。

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

今天学到了面向切面编程aop的第二种方法 自定义切面类
导入依赖的步骤以及ApplicationContext.xml的步骤和上一种方法一致
依赖如下

 <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.0.RELEASE</version>
        </dependency>

ApplicationContext.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:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">
    <bean id="beforeLog" class="com.zyy.log.BeforeLog"/>
    <bean id="afterLog" class="com.zyy.log.AfterLog"/>
    <bean id="userServiceImpl" class="com.zyy.service.UserServiceImpl"/>
    <bean id="diy" class="com.zyy.log.Log"/>
<!--    <aop:config>-->
<!--        <aop:pointcut id="pointcut" expression="execution(* com.zyy.service.UserServiceImpl.*(..))"/>-->
<!--        <aop:advisor advice-ref="beforeLog" pointcut-ref="pointcut"/>-->
<!--        <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>-->
<!--    </aop:config>-->
    <aop:config>
        <aop:aspect ref="diy">
            <aop:pointcut id="point" expression="execution(* com.zyy.service.UserServiceImpl.*(..))"/>
            <aop:after method="frist" pointcut-ref="point"/>
            <aop:before method="last" pointcut-ref="point"/>
        </aop:aspect>
    </aop:config>
</beans>

上面注释掉的是上一种方法的aop 可以和这种方法对比学习

关于这段代码的理解

<aop:config>
        <aop:aspect ref="diy">
            <aop:pointcut id="point" expression="execution(* com.zyy.service.UserServiceImpl.*(..))"/>
            <aop:after method="frist" pointcut-ref="point"/>
            <aop:before method="last" pointcut-ref="point"/>
        </aop:aspect>
    </aop:config>

这里面的pointcut和上一种方法一样是切入点 id是设置的切入点的名字,后面的表达式则是规定了哪些方法执行前后会触发这个。
和上一种方法不一样,这种方法的触发时间是通过<aop:after或者<aop:before来设置的 上一种则是通过提前创建好了对象,这两个对象实现了两个接口 用接口里面的方法来控制触发时间

测试

import com.zyy.service.USerService;
import com.zyy.service.UserServiceImpl;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    public static void main(String[] args) throws BeansException {
        ApplicationContext Context = new ClassPathXmlApplicationContext("applicationContext.xml");
        USerService userService = Context.getBean("userServiceImpl", USerService.class);
        userService.update();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值