Spring aop常用的两种方式

准备

使用Maven引入依赖包

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.zz</groupId>
        <artifactId>spring-aop</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <properties>
            <spring.version>4.1.4.RELEASE</spring.version>
            <spring-aspects.version>4.1.4.RELEASE</spring-aspects.version>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${spring.version}</version>
            </dependency>
        
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aspects</artifactId>
                <version>${spring-aspects.version}</version>
            </dependency>
        </dependencies>
    </project>

基于@Aspect注解

首先声明一个接口

    public interface Car {
      public void drive();
    }

添加实现类

    @Component( "bus" )
    public class Bus implements Car {

      @Override
      public void drive() {
        System.out.println("开公交车啦");
      }

    }

添加增强类

    @Aspect
    @Component
    public class BusAdvice {
      @Pointcut( "execution(* *.drive(..))" )
      public void pointCut() {};

      @Before( "pointCut()" )
      public void before( JoinPoint joinPoint ) {
        System.out.println("启动发动机~~");
      }

      @After( "pointCut()" )
      public void after( JoinPoint jointPoint ) {
        System.out.println("关闭发动机~~~");
      }
    }

在Spring的配置文件中启动注解和自动代理。

    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
        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-4.1.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-4.1.xsd
            ">
        <!-- 启动注解扫描和自动代理 -->
        <context:component-scan base-package="com.zz.spring.aop2"></context:component-scan>
        <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
    </beans>

Main函数:

    public class Main {
      public static void main( String[] args ) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext2.xml");
        Car car = (Car) ctx.getBean("bus");
        car.drive();
      }
    }

执行结果:

执行结果

注入形式的Aspcet切面

声明一个接口

    public interface Car {
      public void drive();
    }

添加另外一个实现类

    public class AutoMobile implements Car {
      @Override
      public void drive() {
        System.out.println("开汽车啦");
      }
    }

添加增强类

    public class CarAdvice {
      public void before( JoinPoint joinPoint ) {
        System.out.println("启动发动机~~");
      }
      public void after( JoinPoint jointPoint ) {
        System.out.println("关闭发动机~~~");
      }
    }

在Spring配置文件中配置AOP功能

    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns:p="http://www.springframework.org/schema/p"
        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-4.1.xsd">
           <!-- 被代理的Bean -->
          <bean id="autoMobile" class="com.zz.spring.aop1.AutoMobile"></bean>
          <!-- Advice -->
          <bean id="carAdvice" class="com.zz.spring.aop1.CarAdvice"></bean>
          <!-- AOP 配置 -->  
          <aop:config>
            <aop:aspect ref="carAdvice">
                <aop:pointcut expression="execution(* *.drive(..))" id="pointcut"/>
                <aop:before method="before" pointcut-ref="pointcut"/>
                <aop:after method="after" pointcut-ref="pointcut"/>
            </aop:aspect>
          </aop:config>
    </beans>

Main函数

    public class Main {
      public static void main( String[] args ) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext1.xml");
        Car car = (Car) ctx.getBean("autoMobile");
        car.drive();
      }
    }

执行结果

执行结果

代码下载

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,993评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,973评论 6 342
  • 假以时日我卖了自己 我仍会把自己当成宝 晚上独自癫痫 白天梦中消遣 饿了我就啃字玑 闲了我就马路嗑瓜子 废物一般的...
    幽明m阅读 442评论 0 3
  • 我堂姐马上要订婚了,在农村,27还没有结婚对象是一件“丢脸”的事情,没有男朋友的时候,大家都帮忙介绍,后来有了男朋...
    惊酒阅读 346评论 0 0
  • 使用 Properties 在上面的文章中,我已经介绍了在 Java 中如何使用 MySQL, 下面就来看看怎么使...
    ghwaphon阅读 1,061评论 0 3