SpringA o p

SpringAOP面向切面编程

一、Aop术语

通知、连接点、切点、切面、引入、织入

二、Spring对Aop的支持

1、基于代理的经典SpringAOP;
2、纯POJO切面;
3、@AspectJ注解驱动的切面;
4、注入式AspectJ切面(适用于spring的各个版本)

三、spring面向切面编程中的事务处理

1、编写MyMathCalcultor接口

package com.yuer.inter;

public interface MyMathCalcultor {

    public int add(int i, int j);

    public int sub(int i, int j);

    public int mul(int i, int j);  //乘法运算

    public int div(int i, int j);  //除法运算

2、接口的实现类Calcultor

package com.yuer.impl;

import com.yuer.inter.MyMathCalcultor;
import org.springframework.stereotype.Service;

@Service
public class Calculator implements MyMathCalcultor {
    @Override
    public int add(int i, int j) {
        int result = i + j;
        return result;
    }

    @Override
    public int sub(int i, int j) {
        int result = i - j;
        return result;
    }

    @Override
    public int mul(int i, int j) {
        int result = i * j;
        return result;
    }

    @Override
    public int div(int i, int j) {
        int result = i / j;
        return result;
    }
}

通过@Service注解将Calculator类加入到ioc容器中。
3、切面编写

package com.yuer.util;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Aspect
@Component
    public class LogUtils {
/*
通知注解
@Before :前置通知
@After:后置通知
@AfterReturning:返回通知
@AfterThrowing:异常通知
@Around:环绕通知

写切入点表达式
 excution(方法名)
 */
@Before( "execution(public int com.yuer.impl.Calculator.*(int,int))")
   public static void logStart(JoinPoint joinPoint){
   Object[] args = joinPoint.getArgs();
   Signature signature = joinPoint.getSignature();
   String name = signature.getName();
        System.out.println( name + "方法开始执行,用的参数列表:" + Arrays.asList(args));

    }
     @AfterReturning(value = "execution(public int com.yuer.impl.Calculator.*(int,int))",returning = "result")
        public static void logReturn (JoinPoint joinPoint,Object result){
        Object[] args = joinPoint.getArgs();
        Signature signature = joinPoint.getSignature();
        String name = signature.getName();
        System.out.println( name +"法执行完成,计算结果是:" + result);

    }
    @AfterThrowing("execution(public int com.yuer.impl.Calculator.*(int,int))")
    public static void logException(JoinPoint joinPoint){
       Object[] args = joinPoint.getArgs();
       Signature signature = joinPoint.getSignature();
       String name = signature.getName();
       System.out.println(joinPoint.getSignature().getName() +"方法执行异常,异常信息是:" + Arrays.asList(args));
    }
    @After("execution(public int com.yuer.impl.Calculator.*(int,int))")
    public static void logEnd(JoinPoint joinPoint){
        Signature signature = joinPoint.getSignature();
        String name = signature.getName();
        System.out.println(  name + "方法最终结束了");
    }

}

使用@Aspect注解表明LogUtil不仅是一个POJO,还是一个切面;
使用@Component注解将切面添加到ioc容器中;
execution(public int com.yuer.impl.Calculator.*(int,int))切入点表达式。
4、配置ioc容器
第一种基于注解的配置:


```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.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="com.yuer"></context:component-scan>
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

通过扫描获取添加到ioc容器中的类;
aop:aspectj-autoproxy启用AspectJ自动代理。
第二基于xml配置的



   
    <bean id="LogUtils" class="com.yuer.util.LogUtils">bean>
    <bean id="Calculator" class="com.yuer.impl.Calculator">bean>
    
      
    
        <aop:config >
        
            
    
            <aop:aspect ref="LogUtils">
            <aop:pointcut id="performance" expression="execution(public int com.yuer.impl.Calculator.*(int,int))">aop:pointcut>
                <aop:before pointcut-ref="performance" method="logStart">aop:before>
                <aop:after-returning pointcut-ref="performance" method="logReturn" returning="result">aop:after-returning>
                <aop:after-throwing pointcut-ref="performance" method="logException">aop:after-throwing>
                <aop:after  pointcut-ref="performance" method="logEnd">aop:after>

            aop:aspect>
        aop:config>
       -->

你可能感兴趣的:(spring,aop,java)