Spring 使用自定义类实现AOP


友情链接:
AOP是什么?
使用原生API接口实现AOP
使用自定义类实现AOP
使用注解实现AOP

一、搭建环境

模拟service层,首先创建个UserService接口:

package com.wzq.service;

public interface UserService {
    
    
    void add();
    void delete();
    void update();
    void query();
}

UserService接口的实现类:

package com.wzq.service;

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 query() {
    
    
        System.out.println("查询了一条信息");
    }
}

pom.xml中注入aspectjweaver依赖:

<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.4<ersion>
    <scope>runtime</scope>
</dependency>

二、自定义类

可以直接定义两个方法,一个之前,一个之后

package com.wzq.log;

public class DiyPointCut {
    
    
    public void before(){
    
    
        System.out.println("=========方法执行前=========");
    }
    public void after(){
    
    
        System.out.println("=========方法执行后=========");
    }
}

三、配置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 -->
    <bean id="userService" class="com.wzq.service.UserServiceImpl"/>
    <bean id="diyPointCut" class="com.wzq.log.DiyPointCut" />

    <aop:config>
        <!-- 自定义切面,ref 要引用的类 -->
        <aop:aspect ref="diyPointCut">
            <!-- 切入点 -->
            <aop:pointcut id="point" expression="execution(* com.wzq.service.UserServiceImpl.*(..))"/>
            <!-- 通知 -->
            <aop:before method="before" pointcut-ref="point" />
            <aop:after method="after" pointcut-ref="point" />
        </aop:aspect>
    </aop:config>

</beans>

四、测试

import com.wzq.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

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

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/lesileqin/article/details/113555666
今日推荐