0
点赞
收藏
分享

微信扫一扫

Aspectj AOP实现流程(基于XML)

爱做梦的夏夏 2022-05-01 阅读 58

文章目录


前言

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


一、导入AOP相关坐标

    <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>

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

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

三、创建切面类

public class MyAspect {
    public void before(){
        System.out.println("前置增强....");
    }
}

四、将目标类和切面类的对象创建权交给spring,在applicationContext.xml中配置织入关系

<!--配置命名空间-->
<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>

五、测试代码

@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
举报

相关推荐

0 条评论