栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

基于注解的 AOP 开发

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

基于注解的 AOP 开发

基于注解的 AOP 开发

文章目录
  • 基于注解的 AOP 开发
    • 1.步骤
    • 2.注解通知的类型
    • 3.切点表达式的抽取

1.步骤

基于注解的aop开发步骤:
① 创建目标接口和目标类(内部有切点)
② 创建切面类(内部有增强方法)
③ 将目标类和切面类的对象创建权交给 spring
④ 在切面类中使用注解配置织入关系
⑤ 在配置文件中开启组件扫描和 AOP 的自动代理
⑥ 测试

1.目标类和接口

package com.itspring.proxy.anno;

public interface TargetInterface1 {
    void save();

    void update();
}

package com.itspring.proxy.anno;

import org.springframework.stereotype.Component;

@Component("target")  //将目标类注入Spring容器
public class Target implements TargetInterface1 {
    public void save() {
        System.out.println("save running...");
    }

    public void update() {
        System.out.println("update running...");
    }
}

2.切面类

package com.itspring.proxy.anno;

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

@Component("MyAspect")  //将切面类注入Spring容器
@Aspect  //标注当前MyAspect是一个切面类
public class MyAspect {

    //为Target类的所有方法添加前置通知
    @Before(value = "execution(* com.itspring.proxy.anno.Target.*(..))")
    public void before() {
        System.out.println("前置增强...");
    }

    //为Target类的save方法添加后置通知
    @AfterReturning(value = "execution(* com.itspring.proxy.anno.Target.save())")
    public void afterRunning() {
        System.out.println("后置增强...");
    }

    //为Target的update方法添加环绕通知
    @Around(value = "execution(* com.itspring.proxy.anno.Target.update())")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("环绕前增强...");
        Object proceed = pjp.proceed(); //切点方法
        System.out.println("环绕后增强...");
        return proceed;
    }
}

3.配置文件




    
    
    
    

4.测试

package com.itspring.proxy.anno;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-anno.xml")
public class AnnoTest {

    @Autowired
    private TargetInterface1 targetInterface1;

    @Test
    public void test() {
        targetInterface1.save();
        targetInterface1.update();
    }

}

2.注解通知的类型

通知的配置语法: @通知注解(“切点表达式")

3.切点表达式的抽取

同 xml 配置 aop 一样,我们可以将切点表达式抽取。抽取方式是在切面内定义方法,在该方法上使用@Pointcut
注解定义切点表达式,然后在在增强注解中进行引用。具体如下:

@@Component("myAspect")
@Aspect
public class MyAspect {
	@Before("MyAspect.myPoint()")
	public void before(){
	System.out.println("前置代码增强.....");
} 
	@Pointcut("execution(* com.itheima.aop.*.*(..))")
	public void myPoint(){}
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/344781.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号