Spring示例:第一个AOP

首先有如下两个关于开发过程的概念:

  • 纵向编程,代码较为重复
  • 横向编程,重复代码形成切面织入节点

下面编写实例:

  1. 添加额外倚赖
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.9.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.1</version>
        </dependency>

  1. 新建类
public class BeforeAdvice {

    public void methodBefore(){
        System.out.println("我在方法之前执行");
    }

}

public class ProviderService {
    public void add(){
        System.out.println("添加了一个供应商");
    }
}

  1. 配置文件
<?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:c="http://www.springframework.org/schema/c"
       xmlns:cache="http://www.springframework.org/schema/cache"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jdbc="http://www.springframework.org/schema/jdbc"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xmlns:lang="http://www.springframework.org/schema/lang"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:task="http://www.springframework.org/schema/task"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
		http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.3.xsd
		http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd ">


    <!--【第一步:激活代理】 aop是基于代理完成的,所以我们要激活我们的自动代理-->
    <aop:aspectj-autoproxy/>

    <!--【第二步:注册一个切面要使用的类】-->
    <bean class="com.zt.advice.BeforeAdvice" id="beforeAdvice">
    </bean>

    <bean class="com.zt.service.ProviderService" id="providerService">
    </bean>

    <!--【第三步:配置切入点等等信息】-->
    <aop:config >
        <aop:aspect id="beforeAspect" ref="beforeAdvice">
            <!--
                aop:before 表明它是前置通知
                method 指明了它是哪个方法来切
                pointcut:切入点
                        *包下的*类*方法
            -->
            <aop:before method="methodBefore" pointcut="execution(* com.zt.service.ProviderService(..))"></aop:before>
        </aop:aspect>
    </aop:config>


</beans>

带张图解如下:
在这里插入图片描述
关于execution部分的解释如下:
在这里插入图片描述
在这里插入图片描述
4. 测试

@Test
    public void m1(){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring/applicationContext.xml");

        ctx.getBean("beforeAdvice", BeforeAdvice.class);
        //如果不是spring容器管理的bean,那么织入行为无法生效
       // ProviderService providerService = new ProviderService();
        /*ProviderService providerService = ctx.getBean("providerService", ProviderService.class);
        providerService.add();*/
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值