Aspectj AOP实现流程(基于XML)

本文详细介绍了如何使用Spring AOP进行面向切面编程,包括导入相关依赖、创建目标接口、实现目标类和切面类,然后在Spring配置文件中配置织入关系,并通过测试验证切面的效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


前言

AspectJ是一个易用的功能强大的AOP框架
AspectJ全称是Eclipse AspectJ, 其官网地址是:http://www.eclipse.org/aspectj/
 Spring AOP 和 AspectJ 对比
在实验前建议先创建项目骨架
项目骨架

TargetInterface:目标接口
Target:目标对象
MyAspect:切面
applicationContext.xml:配置文件
AopTest:测试类


一、导入AOP相关坐标

pom.xml导入相关坐标

    <dependencies>
    	<!--必需,spring aop相关坐标导入-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.5.RELEASE</version>
        </dependency>
        <!--必需,aspectj aop相关坐标导入-->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.4</version>
        </dependency>
        <!--测试需要-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.0.5.RELEASE</version>
        </dependency>
        <!--测试需要-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>

二、创建目标类和目标接口(内含切点)

创建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,在applicationContext.xml中配置织入关系

创建applicationContext.xml,配置bean

<!--配置命名空间-->
<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"
       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">
    <!--配置目标对象-->
    <bean id="target" class="com.hbh.aop.Target"/>
    <!--配置切面对象-->
    <bean id="myAspect" class="com.hbh.aop.MyAspect"/>
    <!--配置织入-->
    <aop:config>
        <!--声明切面-->
        <aop:aspect ref="myAspect">
        <!--切点+增强-->
            <aop:before method="before" pointcut="execution(public void com.hbh.aop.Target.save())"/>
        </aop:aspect>
    </aop:config>
</beans>

五、测试代码

创建AopTest 类,测试织入效果

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class AopTest {

    @Autowired
    private TargetInterface target;

    @Test
    public void test1(){
        target.save();
    }
}

测试结果

五月 01, 2022 11:07:38 上午 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 11:07:38 上午 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 11:07:38 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
五月 01, 2022 11:07:38 上午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.GenericApplicationContext@c038203: startup date [Sun May 01 11:07:38 CST 2022]; root of context hierarchy

前置增强....
save running...

Process finished with exit code 0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值