AOP

Aop:

要实现Aop,要导入lib/j2ee/common-annotations.jar

在配置文件中需要

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd

几个重要的概念:

切面:其实和类差不多,只是他里面放的都是切入点和前置,后置等通知

切入点:pointCut("execution(* pakage.className.method(..))")

规定要对那个类的方法进行切割

通知:

spring中的注解方法进行AOP的实现:

·注解方式配置AOP

1,在配置文件中写上: 

<aop:aspectj-autoproxy/><!-- 这里就相当于注册了一个aop的处理器 -->

2把切面的那个类和业务方法中的那个我们需要的类交给spring管理,使用在配置文件中使用<bean>来配置,也可以使用包的扫描来配置<context:component-scan base-package="hwt.aspect"/>

3,写一个切面:

@Aspect//切面

public class AspectClass {

@Pointcut("execution(* service.AopService.*(..))")

public void anyMethod(){}//声明一个切入点

@Pointcut("execution(* aop.Service4.*(..))")

public void anyMethod2(){}//可以声明多个切入点

//前置通知,可以得到这个要的调用的这个方法的参数

//一般可以用这个得到要调用的函数的传入参数

@Before("anyMethod() && args(name)"//这个name和下面接收参数一样

public void beforeMethod(String name){

System.out.println("我是前置通知"+name);

}

//后置通知,可以通过returning来得到要调用的方法的返回值

//可以得到要调用的方法的返回值

    // 这里是得到连接点的返回值,名字和下面的一样

@AfterReturning(pointcut="anyMethod()",returning="rs"

public void afterReturningMethod(String rs){

System.out.println("我是后置的方法");

}

//最终通知

@After("anyMethod()")

public void finallyMethod(){

System.out.println("我是最终的通知");

}

//例外通知

@AfterThrowing(pointcut="anyMethod2()",throwing="e")

public void exceptionMethod(RuntimeException e){

System.out.println(e.getMessage());

}

//环绕通知

@Around("anyMethod()")

public Object aroundMethod(ProceedingJoinPoint pjp) throws Throwable{

Object rsObject = null;

System.out.println("进入方法");

rsObject = pjp.proceed();

System.out.println("退出方法");

return rsObject;

}

}

执行的顺序:

前置通知-->环绕通知-->后置通知-->最终通知-->退出环绕通知

在调用方法的时候,会先通过spring产生一个代理对象,来对这个方法进行前后的处理


==================

·XMl中配置Aop

1,写一个普通的java类(就是对于上面的切面,不要写注解的java类,由xml里面来配置)
2,配置文件中可以这样写

//这里是用包的扫描的方法,也可以用配置<bean>的方法

  <context:component-scan base-package="hwt.service"/>

  <context:component-scan base-package="hwt.aspect"/>

//切面的配置

 <aop:config>

     <aop:aspect id="aps" ref="aspectXMLClass">

     <aop:pointcut id="mypt" expression="execution(* hwt.service.PersonService.*(..))"/>

     <!--  前置方法接受参数 -->

     <aop:beforepointcut="execution(* hwt.service.PersonService.*(..))and args(emp)"args-name=”empmethod="beforeMethod"/>

     <aop:around pointcut-ref="mypt" method="aroundMethod"/>

     <aop:after-returning pointcut-ref="mypt" method="afterReturnMethod" returning="results"/>

     <aop:after pointcut-ref="mypt" method="afterMethod"/>

     </aop:aspect>

    </aop:config>



AOP即面向切面编程,是一种编程范式。从概念介绍来看,AOP可用于对程序中原本就存在的业务逻辑方法进行功能增强,通过在这些方法的执行过程中插入通知来实现,被一个或多个切面通知的对象被称作目标对象,在AOP里因使用运行时代理,目标对象永远是被代理的对象[^2][^4]。 在实现机制上,Spring AOP使用动态代理。动态代理指AOP框架不会修改字节码,而是每次运行时在内存中临时为方法生成一个包含目标对象全部方法的AOP对象,该对象会在特定切点做增强处理,并回调原对象的方法[^3]。 AOP在实际应用中有着广泛的用途。例如可以用于日志记录,在方法执行前后记录相关日志信息;进行事务管理,在方法执行前后进行事务的开启、提交或回滚等操作;还能用于权限验证,在方法执行前验证用户是否具有相应的权限等。 ### 示例代码 以下是一个简单的Spring AOP示例代码,展示了如何使用AOP进行日志记录: ```java import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; @Aspect @Component public class LoggingAspect { // 定义切入点,这里表示匹配所有Service类中的所有方法 @Pointcut("execution(* com.example.service.*.*(..))") public void serviceMethods() {} @Before("serviceMethods()") public void beforeAdvice(JoinPoint joinPoint) { System.out.println("Before method: " + joinPoint.getSignature().getName()); } @After("serviceMethods()") public void afterAdvice(JoinPoint joinPoint) { System.out.println("After method: " + joinPoint.getSignature().getName()); } } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值