前面记录了基于注解版的aop配置,现在记录一下基于xml配置的aop (注解版aop链接地址)
基于xml版的aop配置步骤
- 导入jar包 或者maven依赖
- 将目标对象和切面类加入到ioc容器当中
- 告诉spring那个是切面类
- 在切面类中配置五种通知,并且配置五种通知的运行时机(切入点表达式)
项目架构
Operation 类 (业务类)
public interface Operation
{
// 加法
double add(double i, double j);
// 减法
double subtract(double i, double j);
// 乘法
double mul(double i, double j);
// 除法
double div(double i, double j);
}
OperationImpl 类 (业务的实现类)
public class OperationImpl implements Operation
{
@Override
public double add(double i, double j)
{
return i+j;
}
@Override
public double subtract(double i, double j)
{
return i-j;
}
@Override
public double mul(double i, double j)
{
return i*j;
}
@Override
public double div(double i, double j)
{
return i/j;
}
}
OperationProxy 类 (切面类)
public class OperationProxy
{
public void logbefore(JoinPoint joinPoint){
System.out.println("我是前置通知,目标方法名"+joinPoint.getSignature().getName()+
"\t目标方法的参数个数"+joinPoint.getArgs().length+
"\t参数列表 "+ Arrays.asList(joinPoint.getArgs())
);
}
public void logAfter(JoinPoint joinPoint){
System.out.println("我是后置通知,目标方法名"+joinPoint.getSignature().getName()+
"\t目标方法的参数个数"+joinPoint.getArgs().length+
"\t参数列表 "+ Arrays.asList(joinPoint.getArgs())
);
}
public void logException(JoinPoint joinPoint,Exception e){
System.out.println("我是异常通知,目标方法名"+joinPoint.getSignature().getName()+
"\t目标方法的参数个数"+joinPoint.getArgs().length+
"\t参数列表 "+ Arrays.asList(joinPoint.getArgs())+
"\t目标异常 "+e
);
}
public void logFinal(JoinPoint joinPoint,Object result){
System.out.println("我是最置通知,目标方法名"+joinPoint.getSignature().getName()+
"\t目标方法的参数个数"+joinPoint.getArgs().length+
"\t参数列表 "+ Arrays.asList(joinPoint.getArgs())+
"\t目标方法的返回值"+result
);
}
public Object logAround(ProceedingJoinPoint pjp) throws Throwable
{
Object result = null;
try
{
System.out.println("环绕通知模拟的【前置通知】");
result = pjp.proceed(); //控制着目标方法的执行,相当于目标方法是否执行,该方法的返回值就是目标方法得到返回值
// System.out.println(1 /0 );
System.out.println("环绕通知模拟的【后置通知】 方法的返回值为 "+result+"\t目标方法"+pjp.getSignature().getName()+"\t参数个数 "+pjp.getArgs().length);
}
catch (Exception e)
{
System.out.println("环绕通知模拟的【异常置通知】"+e);
}
finally
{
System.out.println("环绕通知模拟的【最终置通知】");
}
return result;
}
}
SpringAopXmlTest 类 (测试类)
public class SpringAopXmlTest
{
ApplicationContext ioc =new ClassPathXmlApplicationContext("applicationContext.xml");
@Test
public void testAopXml1(){
Operation operation = ioc.getBean("operation", Operation.class);
double add = operation.add(1, 2);
System.out.println("运算结果 : "+ add);
}
}
xml
public class SpringAopXmlTest
{
ApplicationContext ioc =new ClassPathXmlApplicationContext("applicationContext.xml");
@Test
public void testAopXml1(){
Operation operation = ioc.getBean("operation", Operation.class);
double add = operation.add(1, 2);
System.out.println("运算结果 : "+ add);
}
}
maven 依赖
<dependencies>
<!--aop 需要的包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version>
</dependency>
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/net.sourceforge.cglib/com.springsource.net.sf.cglib -->
<dependency>
<groupId>net.sourceforge.cglib</groupId>
<artifactId>com.springsource.net.sf.cglib</artifactId>
<version>2.2.0</version>
</dependency>
<!--spring 核心包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.0.RELEASE</version>
</dependency>
<!-- 日志包-->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
- 导入maven依赖
- 将目标对象和切面类加入到ioc容器当中 (注解版的话使用的是四大功能类注解 并且在xml中配置注解扫描包 四大功能类注解链接地址)
<!-- 将目标对象加载到ioc容器 -->
<bean id="operation" class="com.stone.aopdome.operation.impl.OperationImpl">
</bean>
<!-- 将切面类加载到ioc容器 -->
<bean id="operationProxy" class="com.stone.aopdome.proxy.OperationProxy">
</bean>
- 告诉spring那个是切面类 (配置的方式是在类上添加@Aspect 注解 )
<aop:config>
<!-- aop:aspect 配置谁是切面类 如果有多个切面配置多个aop:aspect -->
<aop:aspect ref="operationProxy">
</aop:aspect>
</aop:config>
- 在切面类中配置五种通知,并且配置五种通知的运行时机(注解版的话就是添加对应的注解 比如 @Before @After 等, 注解版aop链接地址)
<aop:config>
<!-- aop:pointcut全局的切面点表达式 -->
<aop:pointcut id="myPointcut" expression="execution(* com.stone.aopdome.operation.impl.OperationImpl.*(..))"/>
<!-- aop:aspect 配置谁是切面类 如果有多个切面配置多个aop:aspect -->
<aop:aspect ref="operationProxy">
<!-- 在切面类中配置五种通知 -->
<!--
before前置通知 ,after后置通知,after-throwing异常通知,after-returning最终通知,around环绕通知
method通知类方法,
pointcut切入点表达式,
pointcut-ref 引入指定的切面的表达式
-->
<aop:before method="logbefore" pointcut="execution(* com.stone.aopdome.operation.impl.OperationImpl.*(..))"/>
<aop:after method="logAfter" pointcut-ref="myPointcut"/>
<aop:after-throwing method="logException" pointcut-ref="myPointcut" throwing="e"/>
<aop:after-returning method="logFinal" pointcut-ref="myPointcut" returning="result"/>
<aop:around method="logAround" pointcut-ref="myPointcut"/>
</aop:aspect>
</aop:config>
测试类
public class SpringAopXmlTest
{
ApplicationContext ioc =new ClassPathXmlApplicationContext("applicationContext.xml");
@Test
public void testAopXml1(){
Operation operation = ioc.getBean("operation", Operation.class);
double add = operation.add(1, 2);
System.out.println("运算结果 : "+ add);
}
}
运行结果