Spring框架中的AOP技术----注解方式

利用AOP技术注解的方式对功能进行增强

CustomerDao接口

复制代码
1 package com.alphajuns.demo1;
2 
3 public interface CustomerDao {
4 
5     public void save();
6     
7     public void update();
8     
9 }
复制代码

CustomerDaoImpl实现类

复制代码
 1 package com.alphajuns.demo1;
 2 
 3 public class CustomerDaoImpl implements CustomerDao {
 4 
 5     @Override
 6     public void save() {
 7         System.out.println("保存客户...");
 8     }
 9 
10     @Override
11     public void update() {
12         System.out.println("更新客户...");
13     }
14 
15 }
复制代码

切面类

复制代码
 1 package com.alphajuns.demo1;
 2 
 3 import org.aspectj.lang.ProceedingJoinPoint;
 4 import org.aspectj.lang.annotation.After;
 5 import org.aspectj.lang.annotation.Around;
 6 import org.aspectj.lang.annotation.Aspect;
 7 import org.aspectj.lang.annotation.Before;
 8 import org.aspectj.lang.annotation.Pointcut;
 9 
10 /*
11  * 注解方式的切面类
12  */
13 @Aspect
14 public class MyAspectAnno {
15 
16     /*
17      * 前置通知
18      */
19     @Before(value="execution(public void com.alphajuns.demo1.CustomerDaoImpl.save())")
20     public void log() {
21         System.out.println("记录日志...");
22     }
23     
24     /*
25      * 后置通知
26      */
27     /*@After(value="execution(public void com.alphajuns.demo1.CustomerDaoImpl.save())")*/
28     @After(value="MyAspectAnno.fn()")
29     public void after() {
30         System.out.println("更新日志...");
31     }
32     
33     @Around(value="execution(public void com.alphajuns.demo1.CustomerDaoImpl.save())")
34     public void around(ProceedingJoinPoint joinPoint) {
35         System.out.println("环绕通知1...");
36         try {
37             joinPoint.proceed();
38         } catch (Throwable e) {
39             e.printStackTrace();
40         }
41         System.out.println("环绕通知2...");
42     }
43     
44     /*
45      * 自定义切入点
46      */
47     @Pointcut(value="execution(public * com.alphajuns.demo1.CustomerDaoImpl.save())")
48     public void fn(){};
49     
50 }
复制代码

测试类

复制代码
 1 package com.alphajuns.demo1;
 2 
 3 import javax.annotation.Resource;
 4 
 5 import org.junit.Test;
 6 import org.junit.runner.RunWith;
 7 import org.springframework.test.context.ContextConfiguration;
 8 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 9 
10 @RunWith(SpringJUnit4ClassRunner.class)
11 @ContextConfiguration("classpath:applicationContext.xml")
12 public class Demo1 {
13 
14     @Resource(name="customerDao")
15     private CustomerDao customerDao;
16     
17     @Test
18     public void run1() {
19         customerDao.save();
20     }
21     
22 }
复制代码

配置文件

复制代码
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:aop="http://www.springframework.org/schema/aop"
 6     xmlns:tx="http://www.springframework.org/schema/tx"
 7     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 8     http://www.springframework.org/schema/beans/spring-beans.xsd
 9     http://www.springframework.org/schema/context
10     http://www.springframework.org/schema/context/spring-context.xsd
11     http://www.springframework.org/schema/aop
12     http://www.springframework.org/schema/aop/spring-aop.xsd
13     http://www.springframework.org/schema/tx 
14     http://www.springframework.org/schema/tx/spring-tx.xsd">
15     
16     <!-- 开启自动代理 -->
17     <aop:aspectj-autoproxy/>
18     
19     <!-- 创建目标对象 -->
20     <bean id="customerDao" class="com.alphajuns.demo1.CustomerDaoImpl"/>
21     
22     <!-- 创建切面类 -->
23     <bean id="myAspectAnno" class="com.alphajuns.demo1.MyAspectAnno"/>
24     
25 </beans>
复制代码

 

posted @   AlphaJunS  阅读(200)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示