Spring 实现AOP编程的两种实现方法 --- 1、基于注解方式进行AOP开发。2、基于XML配置方式进行AOP开发
1、基于注解方式开发AOP --- 使用注解需在applicationContext.xml中引入aop命名空间(红色部分)
<?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-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<aop:aspectj-autoproxy /> <!--启用@Aspect注解的支持-->
<bean id="porsonService" class="com.service.impl.PorsonServiceImpl" />
</beans>
定义切面,将基将于Spring进行管理,在xml中加入<bean id="interceptor" class="com.interceptor.MyInterceptor" />
@Aspect //此注解是将类声明为切面
public class MyInterceptor {
@Pointcut("execution(* com.service.impl.PorsonServiceImpl.*(..))")//execution代表执行业务方法时进行拦截,* 代表返回值的类型 *表示任何返回类型,对此包下的类进行拦截,(..)代表方法的参数随意
private void anyMethod(){} //声明一个切入点
@Before("anyMethod() && args(name)")//带输入参数
private void doAccessCheck(String name)
{
System.out.println("前置通知"+name);
}
@AfterReturning(pointcut="anyMethod()",returning="revalue")//带返回结果
public void doReturnCheck(String revalue)
{
System.out.println("后置通知"+revalue);
}
@After("anyMethod()")//定义最终通知
public void After()
{
System.out.println("最终通知");
}
@AfterThrowing(pointcut="anyMethod()",throwing="e")
public void doAfterThrowing(Exception e)
{
System.out.println("异常通知");
System.out.println(e);
}
@Around("anyMethod()")//环绕通知
public Object doBasicProfiling(ProceedingJoinPoint pjp)throws Throwable
{
System.out.println("进入方法");
Object result=pjp.proceed();
System.out.println("退出方法");
return result;
}
}
客户端测试代码
package com.junit;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.service.PorsonService;
public class SpringAOPTest {
@Test public void interceptorTest()
{
ApplicationContext con=new ClassPathXmlApplicationContext("applicationContext.xml");
PorsonService porsonService=(PorsonService)con.getBean("porsonService");
porsonService.save("Smiles");
porsonService.get(141);
}
}
2、基于XML配置方式开发AOP --- applicationContext.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<bean id="porsonService" class="com.service.impl.PorsonServiceImpl" />
<!-- 基于XML的AOP编程 -->
<bean id="xmlinterceptor" class="com.interceptor.XMLInterceptor" />
<aop:config>
<aop:aspect id="asp" ref="xmlinterceptor">
<!-- execution(* com.service.impl.PorsonServiceImpl.*(java.lang.String,..)) -->
<!-- 方法返回类型 ALL 参数类型-->
<!-- <aop:pointcut id="mycat"
expression="execution(* com.service.impl.PorsonServiceImpl.*(java.lang.String,..))" />
-->
<aop:pointcut id="mycat"
expression="execution(!void com.service.impl.PorsonServiceImpl.*(..))" />
<aop:before pointcut-ref="mycat" method="doAccessCheck"/>
<aop:after-returning pointcut-ref="mycat" method="doReturnCheck"/>
<aop:after-throwing pointcut-ref="mycat" method="doAfterThrowing"/>
<aop:after pointcut-ref="mycat" method="After"/>
<aop:around pointcut-ref="mycat" method="doBasicProfiling"/>
</aop:aspect>
</aop:config>
</beans>
用于切面的普通类,将基交于spring管理
package com.interceptor;
import org.aspectj.lang.ProceedingJoinPoint;
public class XMLInterceptor {
private void doAccessCheck() {
System.out.println("XML前置通知");
}
public void doReturnCheck() {
System.out.println("XML后置通知");
}
public void After() {
System.out.println("XML最终通知");
}
public void doAfterThrowing() {
System.out.println("XML异常通知");
}
public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("XML进入方法");
Object result = pjp.proceed();
System.out.println("XML退出方法");
return result;
}
}
客户端测试代码与基于注解的一样
分享到:
相关推荐
Spring的AOP支持两种代理类型:JDK动态代理和CGLIB代理。JDK动态代理适用于实现了接口的目标对象,它会为这些对象创建一个实现了相同接口的代理对象。而CGLIB代理则是在运行时为目标类生成一个子类,如果目标类没有...
在Spring中,AOP的实现有两种方式:基于代理的和基于注解的。基于代理的方式分为JDK动态代理和CGLIB代理。JDK动态代理适用于接口实现类,而CGLIB代理则针对没有实现接口的类。基于注解的AOP更易于使用,我们只需在...
在Spring框架中,AOP的实现有两种主要方式:一种是基于XML配置,另一种是基于注解。本篇将主要讨论如何通过注解方式来实现AOP编程。 首先,我们需要了解Spring中的核心注解。`@Aspect`是定义一个切面的注解,通常会...
在 Spring 中,AOP 的实现主要依赖于代理模式,有两种代理方式:JDK 动态代理和 CGLIB 动态代理。 JDK 动态代理是基于接口的,它要求被代理的目标对象必须实现至少一个接口。Spring 使用 `java.lang.reflect.Proxy`...
面向切面编程(AOP)是一种编程范式,旨在将横切关注点(如日志、安全等)与业务逻辑分离,从而提高模块化。AOP通过预定义的“切面”对横切关注点进行模块化,从而可以在不修改业务逻辑代码的情况下增加新功能。动态...
【Spring AOP实现方法大全】 在Spring框架中,面向切面编程(Aspect-Oriented Programming,简称AOP)是一种强大的设计模式,它允许我们在不修改业务代码的情况下,插入额外的功能,比如日志记录、事务管理等。在...
Spring提供了两种类型的代理:JDK动态代理和CGLIB代理。 8. **编织(Weaving)**:是指将通知插入到目标对象的过程。Spring AOP是在运行时完成的织入过程,即在程序运行时动态地添加通知。 #### 三、Spring AOP的...
本示例提供了一种通过注解和配置文件两种方式实现Spring AOP的方法。 首先,我们来详细讲解通过注解实现Spring AOP。在Spring中,我们可以使用`@Aspect`注解来定义一个切面,这个切面包含了多个通知(advice),即...
- **Spring AOP 配置方法**:详细介绍使用 XML 和注解两种方式进行配置的方法。 - **切入点 (Pointcut)**:定义切面应该应用于哪些连接点的规则。 - **增强 (Advice)**:描述在指定连接点发生时应执行的操作。 - **...
有两种代理方式:JDK动态代理和CGLIB代理。JDK动态代理适用于接口实现类,它通过反射机制在运行时生成实现了目标接口的代理类;而CGLIB代理则适用于无接口或非接口实现类,它通过字节码技术创建目标类的子类来实现...
在Spring框架中,面向切面编程(Aspect Oriented Programming,简称AOP)是一种强大的设计模式,它允许我们定义横切关注点,如日志、事务管理、权限检查等,然后将这些关注点与核心业务逻辑解耦。这篇教程将详细讲解...
在Spring AOP中,有两种动态代理方式:JDK动态代理和CGLIB动态代理。JDK代理适用于接口实现类,而CGLIB代理则用于没有接口或需要对类进行增强的情况。在实验中,使用了基于XML和注解的AspectJ实现。AspectJ是一种...
Spring AOP主要通过两种方式实现:JDK动态代理和CGLIB代理。 - **JDK动态代理**: - 当目标对象实现了至少一个接口时,Spring会使用JDK的java.lang.reflect.Proxy类创建一个代理对象。 - 代理对象在调用实际方法...
在本篇关于“Spring基础:AOP编程(2)”的文章中,我们将深入探讨Spring框架中的面向切面编程(Aspect-Oriented Programming, AOP),这是一种强大的设计模式,它允许我们分离关注点,尤其是那些横切关注点,如日志、...
Spring AOP有两种实现方式:代理模式和注解驱动。代理模式分为JDK动态代理和CGLIB代理。JDK动态代理适用于实现了接口的目标对象,它通过实现InvocationHandler接口创建代理对象。而CGLIB代理则是在运行时为类生成...
Spring 提供了两种方式来实现AOP代理:JDK动态代理和CGLIB代理。 1. **JDK动态代理**: - JDK动态代理基于Java的接口实现,适用于目标对象实现了接口的情况。Spring会为这个接口创建一个代理类,代理类在调用真实...
在Spring中,AOP主要通过两种方式实现:代理模式(包括JDK动态代理和CGLIB代理)以及基于注解的AOP配置。 1. **代理模式**: - JDK动态代理:适用于实现了接口的类,Spring会创建一个实现了相同接口的代理类,拦截...
在Spring中,AOP的实现主要有两种方式:基于代理的AOP(Proxy-based AOP)和基于字节码的AOP(Bytecode-based AOP)。基于代理的AOP主要使用JDK动态代理或CGLIB库来创建代理对象,而基于字节码的AOP则使用AspectJ库...
在Spring框架中,面向切面编程(AOP)是一种强大的设计模式,它允许开发者将关注点从业务逻辑中分离出来,比如日志记录、事务管理等。AspectJ是Java平台上的一个开源AOP框架,提供了丰富的语法来定义切面。本教程将...
- **代理(Proxy)**:Spring AOP通过代理对象实现切面的织入,有JDK动态代理和CGLIB代理两种方式。 3. **Spring AOP的工作原理** 当应用程序执行到达切点时,Spring会创建一个代理对象,这个代理对象会在目标...