SpringAop小例

面向切面编程(也叫面向方面编程)Aspect Oriented Programming(AOP)
Spring AOP  实例  
1 ,前置通知;
2 ,后置通知;
3 ,环绕通知;
4 ,返回通知;
5 ,异常通知; 
——————————————————————————————————————
  导入JAR包
——————————————————————————————————————
   

——————————————————————————————————————
beans.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
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
 
<bean id="studentServiceAspect" class="com.java1234.advice.StudentServiceAspect"></bean>

<bean id="studentService" class="com.java1234.service.impl.StudentServiceImpl"></bean>
 <aop:config>
 <aop:aspect id="studentServiceAspect" ref="studentServiceAspect">
                <aop:pointcut expression="execution(* com.java1234.service.*.*(..))" id="businessService"/>
 <aop:before method="doBefore" pointcut-ref="businessService"/>
 <aop:after method="doAfter" pointcut-ref="businessService"/>
 <aop:around method="doAround" pointcut-ref="businessService"/>
 <aop:after-returning method="doAfterReturning" pointcut-ref="businessService"/>
 <aop:after-throwing method="doAfterThrowing" pointcut-ref="businessService" throwing="ex"/>
 </aop:aspect> 
 </aop:config>
</beans>


——————————————————————————————————————
package com.java1234.service.impl;
import com.java1234.service.StudentService;
public class StudentServiceImpl implements StudentService{

 @Override
 public void addStudent(String name) {
 // System.out.println("开始添加学生"+name);
 System.out.println("添加学生"+name);
 //System.out.println(1/0);
 // System.out.println("完成学生"+name+"的添加");
 }
}
——————————————————————————————————————
package com.java1234.advice;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

public class StudentServiceAspect {

 public void doBefore(JoinPoint jp){
 System.out.println("类名:"+jp.getTarget().getClass().getName());
 System.out.println("方法名:"+jp.getSignature().getName());
 System.out.println("开始添加学生:"+jp.getArgs()[0]);
 }
 
 public void doAfter(JoinPoint jp){
 System.out.println("类名:"+jp.getTarget().getClass().getName());
 System.out.println("方法名:"+jp.getSignature().getName());
 System.out.println("学生添加完成:"+jp.getArgs()[0]);
 }
 
 public Object doAround(ProceedingJoinPoint pjp) throws Throwable{
 System.out.println("添加学生前");
 Object retVal=pjp.proceed();
 System.out.println(retVal);
 System.out.println("添加学生后");
 return retVal;
 }
 
 public void doAfterReturning(JoinPoint jp){
 System.out.println("返回通知");
 }
 
 public void doAfterThrowing(JoinPoint jp,Throwable ex){
 System.out.println("异常通知");
 System.out.println("异常信息:"+ex.getMessage());
 }
}


——————————————————————————————————————
执行顺序
前置通知
环绕通知(前置通知)
返回通知
环绕通知(后置通知)
后置通知
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring框架中,可以使用AOP(面向切面编程)来实现对应用程序的横切关注点进行模块化的处理。下面是使用AOPSpring底层示例: 1. 首先,确保你已经在项目中引入了Spring AOP的相关依赖。 2. 定义一个切面类,该类包含了需要在横切关注点上执行的逻辑。切面类通常使用@Aspect注解进行标记,并且包含多个切点和通知。 ```javaimport org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component; @Aspect@Componentpublic class LoggingAspect { @Before("execution(* com.example.myapp.service.*.*(..))") public void beforeAdvice() { System.out.println("Before advice executed!"); } } ``` 在上述示例中,我们定义了一个切面类`LoggingAspect`,并且使用`@Before`注解定义了一个前置通知(在方法执行之前执行)。切点表达式`execution(* com.example.myapp.service.*.*(..))`定义了匹配的目标方法。 3. 在Spring配置文件中启用AOP支持。可以使用XML配置或者基于Java的配置方式。 XML配置方式: ```xml<aop:aspectj-autoproxy/> <context:component-scan base-package="com.example.myapp"/> ``` 基于Java的配置方式: ```java@Configuration@EnableAspectJAutoProxy@ComponentScan("com.example.myapp") public class AppConfig { // 配置其他Bean} ``` 4. 现在,当目标方法匹配切点表达式时,AOP将会自动应用切面的通知。在本例中,当执行`com.example.myapp.service`包中的任何方法时,`beforeAdvice()`方法将会在方法执行之前被调用。 需要注意的是,切面类和目标类都需要被Spring管理,可以使用`@Component`或者相关的注解进行标记。 这是一个简单的Spring底层使用AOP的示例,你可以根据具体的业务需求和场景来定义更复杂的切面和通知。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值