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

Aspectj AOP实现流程(基于注解)

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

Aspectj AOP实现流程(基于注解)

文章目录
  • 前言
  • 一、创建目标接口和目标类(包含切点)
  • 二、创建切面类(包含增强方法)
  • 三、将目标类和切面类的对象创建权交给spring
  • 四、在切面类中使用注解配置织入关系
  • 五、在配置文件中开启组件扫描和AOP的自动代理
  • 六、测试代码


前言

1.maven坐标请到上一篇文章提取,地址:Aspectj AOP实现流程(基于XML)
2.在实验前建议先创建项目骨架

一、创建目标接口和目标类(包含切点)

创建TargetInterface接口

public interface TargetInterface {
    public void save();
}

创建Target类实现TargetInterface接口

public class Target implements TargetInterface {
    public void save() {
        System.out.println("save running...");
    }
}
二、创建切面类(包含增强方法)

创建MyAspect类

public class MyAspect {
    public void before(){
        System.out.println("前置增强....");
    }
}
三、将目标类和切面类的对象创建权交给spring

使用注解标注Target类

@Component("target")
public class Target implements TargetInterface {
    public void save() {
        System.out.println("save running...");
    }
}

使用注解标注MyAspect类

@Component("myAspect")
public class MyAspect {
    public void before(){
        System.out.println("前置增强....");
    }
}
四、在切面类中使用注解配置织入关系

创建MyAspect 类

@Component("myAspect")
@Aspect //标注当前类是切面类
public class MyAspect {
	//配置切入点
    @Before("execution(* com.hbh.anno.*.*(..))")
    public void before(){
        System.out.println("前置增强....");
    }
}
五、在配置文件中开启组件扫描和AOP的自动代理



        
        
        
        

六、测试代码

创建AopTest 类,测试织入效果

五月 01, 2022 3:39:21 下午 org.springframework.test.context.support.AbstractTestContextBootstrapper getDefaultTestExecutionListenerClassNames
信息: Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
五月 01, 2022 3:39:21 下午 org.springframework.test.context.support.AbstractTestContextBootstrapper getTestExecutionListeners
信息: Using TestExecutionListeners: [org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@3a03464, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@2d3fcdbd, org.springframework.test.context.support.DirtiesContextTestExecutionListener@617c74e5]
五月 01, 2022 3:39:21 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
五月 01, 2022 3:39:21 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.GenericApplicationContext@c038203: startup date [Sun May 01 15:39:21 CST 2022]; root of context hierarchy

前置增强....
save running...
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/855474.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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