怎么给某个方法加aop

怎么给某个方法加aop,第1张

您好,给某个方法加AOP,首先需要了解什么是AOP,AOP(面向切面编程)是一种编程技术,它可以帮助开发人员把一些公共的行为抽离出来,并将其封装在一个可重用的模块中,以便在不同的类中复用,从而提高代码的可重用性和可维护性。

要给某个方法加AOP,需要做以下几步:

1. 定义切面:首先需要定义一个切面,切面是一个模块,它包含了一组公共行为,比如日志记录、权限检查等;

2. 定义切入点:切入点是一个指定的方法,它定义了切面应该在什么地方被应用;

3. 定义通知:通知是一个模块,它定义了切面应该在什么时候被应用,比如在方法执行前、执行后或者抛出异常时;

4. 将切面应用到指定的方法上:最后,将切面应用到指定的方法上,以便在方法执行前后执行切面中定义的行为。

以上就是给某个方法加AOP的步骤,希望能够帮助您。

1. 基于xml配置文件的代理配置方式

这种方式在2.0以后很少用了,原因是配置项过多,过于繁琐。但对于理解Spring AOP还是很有帮助的

1.1 定义通知

<bean id="advice" class="yourAdviceImpl" />

1.2 定义切点

要定义一个切点,可以选择使用正则表达式方式声明的切点或者AspectJ方式声明的切点。对正则表达式切点,使用Perl5RegexpMethodPointcut或JdkRegexpMethodPointcut(Java

1.4以上版本,不需要Jakarta ORO的支持了);对AspectJ切点,使用AspectJExpressPointcut

<bean id="pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut">

<property name="pattern" value="yourRegularExpression" />

</bean>

<bean id="pointcut" class="org.springframework.aop.aspectj.AspectJExpressionPointcut">

<property name="expression" value="yourAspectJExpression" />

</bean>

1.3 定义通知者

DefaultPointcutAdvisor是Spring提供的默认通知者,它需要提供通知和切点的引用。

Spring也提供了RegexpMethodPointcutAdvisor和AspectJExpressionPointcutAdvisor来对应两种声明切点的方式,不用再单独定义切点。

<bean id="advisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">

<property name="advice" ref="advice" />

<property name="pointcut" ref="pointcut" />

</bean>

<bean id="advisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">

<property name="advice" ref="advice" />

<property name="pattern" value="yourRegularExpression" />

</bean>

<bean id="advisor" class="org.springframework.aop.aspectj.AspectJExpressionPointcut">

<property name="advice" ref="advice" />

<property name="expression" value="yourAspectjExpression" />

</bean>

1.4 定义ProxyFactoryBean

<bean id="yourBean" class="org.springframework.aop.framework.ProxyFactoryBean>

<property name="target" ref="yourTargetBean" />

<property name="interceptorNames" value="advisor" />

<property name="proxyInterfaces" value="interfaceClass" />

</bean>

interceptorNames和proxyInterfaces都是数组属性,所以可以声明要使用的一个list,也可以让Spring自动把单个值转化为数组

上面明确定义了要对那个targetBean应用代理生成切面实例。如果不想限制targetBean,可以让Spring为所有匹配切点声明的bean生成切面实例,这样就不用一个个定义ProxyFactoryBean了,只需要定义

<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" />

这是一个BeanPostProcessor,所以Spring会自动识别并在bean的声明周期使用

2 利用2.0以后使用aop标签

<aop:config>

<aop:aspect ref="">

<aop:pointcut id="performance" expression="execution(* *.perform(..))" />

<aop:before method="" pointcut-ref="performance" />

<aop:before method="" pointcut="execution(* *.perform(..))" />

<aop:after-returning method="" pointcut="execution(* *.perform(..))" />

<aop:after-throwing method="" pointcut="execution(* *.perform(..))" />

</aop:aspect>

</aop:config>

3 利用Annotation

3.1 利用@Aspect将一个POJO类声明为一个切面。

3.2 定义切点

@Pointcut("execution(* *.perform(..))")

public void performance(){}

通过@Pointcut定义的切点的名字就是它所注解的方法的名字,因此例子中的切点名字是

performance()。这里声明的performance()方法实际圣只是一个标记,为@Pointcut提供附加的点,并不要求有实际意义。

3.3 定义通知

对要执行切面的方法,通过@Before("performance()"),@AfterReturning

("performance()")来定义通知。注意这里提供的切点名称,是performance(),而不是performance

如果对上面的两点不是很理解,也可以省略@Pointcut,而将AspectJ表达式直接定义在@Before等通知中,将上面的两步合为一步,如@Before("execution(* *.perform(..))")

3.4 通知Spring创建代理

<aop:aspectj-autoproxy>

这实际上相当于声明了一个AnnotationAwareAspectJAutoProxyCreator,从而根据@Pointcut声明的切点来自动代理匹配的bean实例

4 在Spring中结合进AspectJ

对于超出Spring AOP支持范围的,可以采用这种方式。只需要在Spring中配置AspectJ的Class实例时让Spring能够获得AspectJ类的实例就可以了,比如

<bean class="a_aspectj_class" factory-method="aspectOf">

<preperty ..../>

</bean>

长安汽车aop连接汽车步骤如下:

1、开启手机的长安汽车软件,找到并点击界面中我的爱车选项。

2、开启的我的爱车界面中,找到并点击界面中的添加车辆选项。

3、开启的添加车辆界面中,输入车辆信息,点击下一步选项,来进行添加车辆即可。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11474872.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-16
下一篇2023-05-16

发表评论

登录后才能评论

评论列表(0条)

    保存