服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Spring Aop注解实现

Spring Aop注解实现

2021-10-14 11:42宁在春 Java教程

本文我们通过Spring AOP和Java的自定义注解来实现日志的插入功能,非常不错,具有一定的参考借鉴价值,需要的朋友一起看看吧,希望对你有所帮助

Spring-aop-理论知识 Spring-Aop-注解实现 项目结构图

Spring Aop注解实现

具体步骤:

1、创建maven 项目 导入依赖 创建好项目结构

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<dependencies>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.18</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.3.4</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>5.3.4</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>5.3.4</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-expression</artifactId>
        <version>5.3.4</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>5.3.4</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.9.6</version>
    </dependency>
</dependencies>

2、写一个接口 及 其实现类

?
1
2
3
4
5
6
7
8
/**
 * @version 1.0
 * @author: crush
 * @date: 2021-03-05 10:26
 */
public interface TestDao {
    public void delete();
}
?
1
2
3
4
5
6
7
8
9
10
11
/**
 * @version 1.0
 * @author: crush
 * @date: 2021-03-05 10:27
 */
@Service
public class TestDaoImpl implements TestDao {
    public void delete() {
        System.out.println("正在执行的方法-->删除");
    }
}

3、切面类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
 * @version 1.0
 * @author: crush
 * @date: 2021-03-10 18:04
 */
@Aspect
@Component
public class MyAspectAnnotation {
    @Pointcut("execution(* com.dao.*.*(..))")
    private void myPointCut(){
    }
    /**
     * 前置通知 使用JoinPoint 接口作为参数获得目标对象的信息
     *    @Before("myPointCut()") ==
     *    <aop:after method="after" pointcut-ref="myPointCut"/>
     **/
    @Before("myPointCut()")
    public void before(JoinPoint jp){
        System.out.print("前置通知:模拟权限控制");
        System.out.println("目标对象:"+jp.getTarget()+",被增强的方法:"+jp.getSignature().getName());
    }
    @AfterReturning("myPointCut()")
    public void afterReturning(JoinPoint jp){
        System.out.print("后置返回通知:"+"模拟删除临时文件");
        System.out.println(",被增强的方法"+jp.getSignature().getName());
    }
    @Around("myPointCut()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("环绕开始:执行目标方法前,模拟开启事务");
        Object obj = pjp.proceed();
        System.out.println("环绕结束:执行目标方法后,模拟关闭事务");
        return obj;
    }
    @AfterThrowing(value = "myPointCut()",throwing = "throwable")
    public void except(Throwable throwable){
        System.out.println("异常通知:"+"程序执行异常"+throwable.getMessage());
    }
 
    @After("myPointCut()")
    public void after(){
        System.out.println("最终通知:释放资源");
    }
 
}

4、application.xml 文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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 https://www.springframework.org/schema/aop/spring-aop.xsd">
        <context:component-scan base-package="com.dao"/>
        <context:component-scan base-package="com.aspect"/>
        <!--启动基于注解的AspectJ支持-->
        <aop:aspectj-autoproxy proxy-target-class="true"/>
</beans>

测试

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Test
public void Test(){
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
    TestDao testDao = applicationContext.getBean("testDaoImpl", TestDaoImpl.class);
    testDao.delete();
    /**
     * 输出:
     * 环绕开始:执行目标方法前,模拟开启事务
     * 前置通知:模拟权限控制目标对象:com.dao.TestDaoImpl@2bef51f2,被增强的方法:delete
     * 正在执行的方法-->删除
     * 后置返回通知:模拟删除临时文件,被增强的方法delete
     * 最终通知:释放资源
     * 环绕结束:执行目标方法后,模拟关闭事务
     */
}

总结

本篇文章就到这里了,希望能给你带来帮助,也希望您能够多多关注服务器之家的更多内容!

原文链接:https://blog.csdn.net/weixin_45821811/article/details/118054678

延伸 · 阅读

精彩推荐