美文网首页
Spirng 中面向切面的编程AOP(使用)

Spirng 中面向切面的编程AOP(使用)

作者: 熊猫的雄 | 来源:发表于2018-10-26 16:41 被阅读0次
AOP有5种通知:

@Before:前置通知,在方法执行之前通知
@After:后置通知,在方法执行之后通知
@AfterRunning:返回通知,在方法返回结果之后通知
@AfterThrowing:异常通知,在方法出现异常之后通知
@Around:环绕通知,环绕着方法通知

AOP开发除了官方下载的包外需要添加两个jar包

com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar

切面 = 通知 + 切入点

这里只讲@Before和@Around
写一个UserDao接口和UserDaoImpl实现类,在实现类中写一个read()方法作为切入点

package com.hello.dao;

public interface UserDao {
    public void read();
}
package com.hello.dao.impl;
import org.springframework.stereotype.Component;
import com.hello.dao.UserDao;

@Component("userDao")
public class UserDaoImpl implements UserDao {
    @Override
    public void read() {
        System.out.println("UserDao中的read()方法");    
    }
}

在src目录下添加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.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
    <!-- 注册注解 -->
    <context:annotation-config/>
    <!-- 扫描注解 -->
    <context:component-scan base-package="com.hello"/>
    <!-- 加载自动代理(切面) -->
    <aop:aspectj-autoproxy/>
</beans> 

生成一个切面Myplus类,在类中写callPlus()和aroundMethod()作为扩展方法

package com.hello.plus;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component("myPlus")
public class Myplus {
    //切面 = 通知 + 切入点
    @Before("execution(public void com.hello.dao.impl.UserDaoImpl.read())")
    public void callPlus() 
        System.out.println("我是最强的。。。");
    }
    
    @Around("Myplus.dian()")
    public void aroundMethod(ProceedingJoinPoint joinPoint){
        System.out.println("around--------前");

        try {
            joinPoint.proceed();
        } catch (Throwable e) {
            e.printStackTrace();
        }

        System.out.println("around--------后");  
    }

    //切入点
    @Pointcut(value="execution(* *..*.*Impl.read())")
    public void qieRuDIan(){
    }
}

1、在callPlus()方法中
@Aspect:注解表示这个类作为一个切面类
@Component("myPlus"):表示将这个类方法IOC中,也就是交给Spring管理

切面 = 通知 + 切入点
@Before:前置通知
("execution(public void com.hello.dao.impl.UserDaoImpl.read())"):切入点

2、在aroundMethod()中
@Around:环绕通知
("Myplus.dIan()"):调用该类中的dian()方法获得切入点

参数 ProceedingJoinPoint joinPoint:声明一个joinPoint对象
joinPoint.proceed():在 read() 执行前执行 joinPoint.proceed() 前的代码,在 read() 执行后执行 joinPoint.proceed() 后的代码。

执行结果:


1.PNG

相关文章

网友评论

      本文标题:Spirng 中面向切面的编程AOP(使用)

      本文链接:https://www.haomeiwen.com/subject/ybaktqtx.html