一个运用sping实现的例子(AOP切片)

1.编写一个业务逻辑接口

2.实现一个逻辑接口

3.编写一个辅助功能接口

4.同时实现一个辅助功能接口,前置,后置接口

5.编写主函数

6.配置xml文件

7.把log4j文件放到src文件夹下

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个使用Spring注解实现AOP例子: 首先,在你的Spring配置文件中,需要开启AOP支持: ``` <bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" /> ``` 然后,创建一个切面类,使用@Aspect注解标注: ``` @Aspect public class LoggingAspect { @Before("execution(* com.example.service.*.*(..))") public void logBefore(JoinPoint joinPoint) { System.out.println("Before executing " + joinPoint.getSignature().getName()); } @AfterReturning(pointcut = "execution(* com.example.service.*.*(..))", returning = "result") public void logAfterReturning(JoinPoint joinPoint, Object result) { System.out.println("After executing " + joinPoint.getSignature().getName() + ", result is " + result); } @AfterThrowing(pointcut = "execution(* com.example.service.*.*(..))", throwing = "exception") public void logAfterThrowing(JoinPoint joinPoint, Exception exception) { System.out.println("Exception thrown from " + joinPoint.getSignature().getName() + ", exception is " + exception); } } ``` 在上面的例子中,我们定义了三个通知,分别在方法执行前、执行后和抛出异常时执行。我们使用@Pointcut注解定义了一个切点,它匹配com.example.service包中的所有方法。然后,在通知方法上使用@Before、@AfterReturning和@AfterThrowing注解,分别表示在方法执行前、执行后和抛出异常时执行。 最后,在你的服务类中,使用@Loggable注解标注需要记录日志的方法: ``` @Service public class MyService { @Loggable public void doSomething() { // ... } } ``` 这样,在执行doSomething方法时,LoggingAspect中定义的通知方法就会被自动调用,从而实现AOP的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值