Aspectj 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的自动代理

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
">
        <!--组件扫描-->
        <context:component-scan base-package="com.hbh.anno"/>
        <!--aop自动代理-->
        <aop:aspectj-autoproxy/>
</beans>

六、测试代码

创建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...
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值