使用注解实现Spring aop

本文通过实战演示如何使用 Spring AOP 进行切面编程,包括配置 Spring 环境、定义切点和通知,以及将切面应用到具体业务逻辑中。

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

 

首先建一个普通Java项目:com.longthsoft.learn.spring

 spring.jar, commons-logging.jar, cglib-nodep-...jar, aspectjweaver.jar, aspectjrt.jar 放到 Build Path .



下面编码开始:

让我们先写两个简单的类:

代码
1 package om.longthsoft.learn.spring.models;   
2       
3 public class A {   
4     public void sayHello() {   
5             System.out.println("Hello, I'm a");   
6         }   
7     }  


1 package com.longthsoft.learn.spring.models;   
中国网管联盟www.bitscn.com
2 public class B {   
3         public void sayHi() {   
4             System.out.println("Hi, I'm b");   
5         }   
6     }   



没什么实际的东西, 只是小A和小B在打招呼

接下来把他们交给Spring吧(有点残忍)。

代码
 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:aop="http://www.springframework.org/schema/aop"  
 5         xsi:schemaLocation="   
中国网管联盟wwwbitsCNcom
 6             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd   
 7             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">  
 8            
 9 <bean id="a" class="com.longthsoft.learn.spring.models.A" />  
10 <bean id="b" class="com.longthsoft.learn.spring.models.B" />  
11 </beans>  
12 

接下来写个Boot

代码
 1     package com.longthsoft.learn.spring;   
 2       
 3     import org.springframework.context.ApplicationContext;   

feedom.net


 4     import org.springframework.context.support.ClassPathXmlApplicationContext;   
 5       
 6     import com.longthsoft.learn.spring.models.A;   
 7     import com.longthsoft.learn.spring.models.B;   
 8       
 9     public final class Boot {   
10       
11         public static void main(String[] args) {   
12             ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");   
13             A a = (A) ctx.getBean("a");   

feedom.net


14             a.sayHello();   
15                
16             B b = (B) ctx.getBean("b");   
17             b.sayHi();   
18         }   
19       
20     }  
21 

嘿, 这里的运行结果不帖了, 大家脑子里闪过即可。

圣诞到了, A介绍完自己之后,也应该说句 "Merry Christmas"

Spring 
说, 既然你们交给我, 这等 routine 就不用再麻烦了, 直接一并处理掉。

于是:

代码
 1     package com.longthsoft.learn.spring;   

中国网管联盟wwwbitsCNcom


 2       
 3     import org.aspectj.lang.annotation.AfterReturning;   
 4     import org.aspectj.lang.annotation.Aspect;   
 5     import org.aspectj.lang.annotation.Pointcut;   
 6       
 7     @Aspect  
 8     public class SimpleAspect {   
 9       
10         @Pointcut("execution(* com.longthsoft.learn.spring.models.*.say*())")   
11         public void simplePointcut() { }   
12            
13         @AfterReturning(pointcut="simplePointcut()")   

网管网bitsCN_com


14         public void simpleAdvice() {   
15             System.out.println("Merry Christmas");   
16         }   
17     }   
18 

然后修改一下配置文件

代码
 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:aop="http://www.springframework.org/schema/aop"  
 5         xsi:schemaLocation="   
中国网管联盟www_bitscn_com
 6             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd   
 7             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">  
 8            
 9         <aop:aspectj-autoproxy />  
10            
11         <bean id="a" class="com.longthsoft.learn.spring.models.A" />  
12         <bean id="b" class="com.longthsoft.learn.spring.models.B" />  
13            
网管网bitsCN.com
14         <bean id="simpleAspect" class="com.longthsoft.learn.spring.SimpleAspect" />  
15     </beans>  
16 

OK
 运行一下:

Hello, I'm a
Merry Christmas
Hi, I'm b
Merry Christmas

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值