1. 既然是在spring中,那么切面的配置肯定是可以通过配置文件配置的啦~~
  2. 语法

<aop:before>

配置目标方法执行之前的处理

<aop:after>

配置目标方法执行之后的处理

<aop:after-returing>

配置after-returing处理

<aop:after-throwing>

配置after-throwing处理

<aop:after-around>

配置after-around处理

  1. <aop:config> aop配置的配置标志 <aop:aspect>配置切面 
  1. 原理,先和普通bean一样的配置方法配置一个bean,然后再把这个普通的bean配置成切面bean
  1. 普通的类,把它在配置文件中按普通的bean一样配置
package pointcut_package;

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

import java.util.Arrays;

public class FourAdviceTest {
    public Object processTx(ProceedingJoinPoint proceedingJoinPoint) throws  java.lang.Throwable{
        System.out.println(" **** around增强,在方法执行之前");
        Object []args = proceedingJoinPoint.getArgs();
        if(args != null && args.length > 0 && args[0].getClass() == String.class){
            args[0] = "  **** 增强的arg " + args[0];
        }
        Object rvt = proceedingJoinPoint.proceed(args);
        System.out.println("  **** around增强,方法执行之后");
        if(rvt !=null && rvt instanceof  Integer){
            rvt = (Integer)rvt * (Integer)rvt;
        }
        return rvt;
    }


    public void authority(JoinPoint joinPoint){
        System.out.println(" **** before增强,方法执行之前");
        System.out.println("  **** before,被织入增强的目标方法为" + joinPoint.getSignature().getName());
        System.out.println(" **** before,  被织入增强的目标方法参数为" + joinPoint.getArgs());
        System.out.println(" **** before, 被织入增强的目标方法对应的目标对象为" + joinPoint.getTarget());
    }


    public void log(JoinPoint joinPoint, Object rvt){
//        这里就直接用啦~
        System.out.println(" **** afterReturning ,获取目标方法的返回值" + rvt);
        System.out.println(" **** afterReturning,模拟记录日志" + joinPoint.getSignature().getName());
        System.out.println(" **** afterReturning, 目标方法的参数为" + joinPoint.getArgs());
        System.out.println(" **** afterReturinig, 目标方法的对应的对象为" + joinPoint.getTarget());
    }


    public void release(JoinPoint joinPoint){
        System.out.println("  **** after, 模拟方法结束之后释放资源");
        System.out.println(" **** after, 被织入的方法是" + joinPoint.getSignature().getName());
        System.out.println(" **** after, 被织入目标方法的参数是 " + Arrays.toString(joinPoint.getArgs()));
        System.out.println(" **** after, 被织入方法对应的目标对象是" + joinPoint.getTarget());
    }
}
  1. 配置文件,详细的讲解穿插在代码里面
<?xml version="1.0" encoding="GBK"?>
<!--要导入zop支持-->
<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-4.0.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
	<!-- 指定自动搜索Bean组件、自动搜索切面类 -->
	<context:component-scan base-package="pointcut_package">
		<context:include-filter type="annotation"
			expression="org.aspectj.lang.annotation.Aspect"/>
	</context:component-scan >
	<!-- 启动@AspectJ支持 -->
	<aop:aspectj-autoproxy/>

<aop:config>
	<aop:aspect id="fourAdviceAspect" ref="fourAdviceBean" order="2">
		<!--讲id为fourAdviceAspect的baen转换成切面bean,并且指定切面bean的id,优先级-->

		<!--直接写入切入点表达式和对应bean中的方法-->
		<aop:after pointcut = "execution(* pointcut_package.*.* (..))" method = "release"/>
		<aop:before pointcut = "execution(* pointcut_package.*.* (..))" method = "authority"/>
		<aop:around pointcut = "execution(* pointcut_package.*.* (..))" method = "processTx"/>
		<aop:after-returning method="log" pointcut="execution(* pointcut_package.*.* (..))" returning="rvt"/>
	</aop:aspect>

	<aop:aspect id="secondAdviceAspect" ref="secondAdviceBean" order="1">
		<!--讲id为fourAdviceAspect的baen转换成切面bean,并且指定切面bean的id,优先级-->
		<aop:after pointcut="execution(* pointcut_package.*.* (..)) and args(aa,..)" method="authority"/>
	</aop:aspect>

</aop:config>


	<!--这些这是普通的bean,在上面被转换成了切面bean-->
	<bean id="fourAdviceBean" class="pointcut_package.FourAdviceTest"/>

	<bean id="secondAdviceBean" class="pointcut_package.SecondAdviceTest"/>



</beans>
  1. 当然啦配置切入点也是不能少的,一并奉上
  1. 语法,要在切面里面定义
<aop:pointcut id="它的id啦" expression="要执行增强的bean">

使用

<aop:before(四种都可以) ...  express-ref="切入点的id">
<?xml version="1.0" encoding="GBK"?>
<!--要导入zop支持-->
<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-4.0.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
	<!-- 指定自动搜索Bean组件、自动搜索切面类 -->
	<context:component-scan base-package="pointcut_package">
		<context:include-filter type="annotation"
			expression="org.aspectj.lang.annotation.Aspect"/>
	</context:component-scan >
	<!-- 启动@AspectJ支持 -->
	<aop:aspectj-autoproxy/>

<aop:config>
	<aop:aspect id="fourAdviceAspect" ref="fourAdviceBean" order="2">
		<!--讲id为fourAdviceAspect的baen转换成切面bean,并且指定切面bean的id,优先级-->

	<aop:pointcut id="myPointcut" expression= "execution(* pointcut_package.*.* (..))"/>

		<!--直接写入切入点表达式和对应bean中的方法-->
		<aop:after pointcut-ref="myPointcut" method = "release"/>
		<aop:before pointcut-ref="myPointcut" method = "authority"/>
		<aop:around pointcut-ref="myPointcut" method = "processTx"/>
		<aop:after-returning method="log" pointcut="execution(* pointcut_package.*.* (..))" returning="rvt"/>
	</aop:aspect>

	<aop:aspect id="secondAdviceAspect" ref="secondAdviceBean" order="1">
		<!--讲id为fourAdviceAspect的baen转换成切面bean,并且指定切面bean的id,优先级-->
		<aop:after pointcut="execution(* pointcut_package.*.* (..)) and args(aa,..)" method="authority"/>
	</aop:aspect>

</aop:config>


	<!--这些这是普通的bean,在上面被转换成了切面bean-->
	<bean id="fourAdviceBean" class="pointcut_package.FourAdviceTest"/>

	<bean id="secondAdviceBean" class="pointcut_package.SecondAdviceTest"/>



</beans>
  1. 这是我看李刚编著的《轻量级javaEE企业应用实战(第五版)-Struts2+Spring5+Hibernate5/JAP2》后总结出来的。