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`...
本示例提供了一种通过注解和配置文件两种方式实现Spring AOP的方法。 首先,我们来详细讲解通过注解实现Spring AOP。在Spring中,我们可以使用`@Aspect`注解来定义一个切面,这个切面包含了多个通知(advice),即...
有两种代理方式:JDK动态代理和CGLIB代理。JDK动态代理适用于接口实现类,它通过反射机制在运行时生成实现了目标接口的代理类;而CGLIB代理则适用于无接口或非接口实现类,它通过字节码技术创建目标类的子类来实现...
在Spring框架中,面向切面编程(Aspect Oriented Programming,简称AOP)是一种强大的设计模式,它允许我们定义横切关注点,如日志、事务管理、权限检查等,然后将这些关注点与核心业务逻辑解耦。这篇教程将详细讲解...
AOP的意思就是面向切面编程。本文主要是通过梳理JDK中自带的反射机制,实现 AOP动态代理模式,这也是Spring AOP 的实现原理
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会创建一个代理对象,这个代理对象会在目标...
在Spring中,AOP通过代理模式实现,有两种代理方式:JDK动态代理和CGLIB代理。JDK动态代理适用于实现了接口的类,而CGLIB代理则是在运行时动态生成子类,用于那些没有实现接口的类。 **1. 创建切面(Aspect)** 切...
Spring.NET AOP提供了两种动态代理机制:基于接口的代理(InterfaceProxy)和基于类型(TypeProxy)。这两种代理都实现了.NET的`System.Linq.Expressions.Expression` API,使得Spring.NET可以在运行时构建和执行...
在Spring中,AOP主要是通过代理模式实现的,有JDK动态代理和CGLIB两种方式。 对于Spring事务管理,我们通常有两种方式:编程式和声明式。编程式事务管理需要在代码中显式地调用begin、commit、rollback等事务控制...
Spring AOP的实现方式主要有两种:静态代理和动态代理。静态代理是开发者手动创建代理类,而动态代理则是在运行时生成代理对象,这正是反射发挥作用的地方。在Java中,可以使用`java.lang.reflect.Proxy`类和`java....