美文网首页
Spring Aop

Spring Aop

作者: 朱芮林 | 来源:发表于2019-03-10 13:48 被阅读0次

    AOP(Aspect Oriented Programming,面向切面编程)通过提供另一种思考程序结构的方式来补充OOP(Object Oriented Programming,面向对象编程)。OOP模块化的关键单元是类,而在AOP中,模块化的单元是切面(Aspect)。切面可以实现跨多个类型和对象之间的事务管理、日志等方面的模块化。
    OOP用继承和组合的方式,编制成一套类和对象的体系;而AOP凡某包某类某命名方法,一并同样处理(正则表达式)。

    AOP核心概念

    1:Aspect(切面)
    2:Join Point(连接点)
    3:Advice(通知/增强)
    4:Pointcut:(切入点)
    5:Introduction(引入)
    6:Target Object(目标对象)
    7:AOP Proxy(AOP代理)
    8:Weaving(织入)

    其中Advice的类型:

    1.Before Adivce(前置通知)
    2.After Returning Advice(返回后通知)
    3.After Throwing Advice(抛出异常后通知)
    4.After(finally)Advice(最后通知)
    5.Around Advice(环绕通知)

    前置增强:

    pom.xml中添加依赖

    <?xml version="1.0" encoding="UTF-8"?>
    
    <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.spring</groupId>
      <artifactId>AoP</artifactId>
      <version>1.0-SNAPSHOT</version>
    
      <name>AoP</name>
      <!-- FIXME change it to the project's website -->
      <url>http://www.example.com</url>
    
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <spring.version>5.1.5.RELEASE</spring.version>
        <aspectj.version>1.9.2</aspectj.version>
        <junit.version>4.12</junit.version>
        <log4j.version>1.2.17</log4j.version>
        <slf4j.version>1.7.12</slf4j.version>
      </properties>
    
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.12</version>
          <scope>test</scope>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>${spring.version}</version>
        </dependency>
        <!--spring-aop依赖-->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-aop</artifactId>
          <version>${spring.version}</version>
        </dependency>
        <!--aspectj依赖-->
        <dependency>
          <groupId>org.aspectj</groupId>
          <artifactId>aspectjrt</artifactId>
          <version>${aspectj.version}</version>
        </dependency>
        <dependency>
          <groupId>org.aspectj</groupId>
          <artifactId>aspectjweaver</artifactId>
          <version>${aspectj.version}</version>
        </dependency>
        <!--spring-test依赖-->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-test</artifactId>
          <version>${spring.version}</version>
        </dependency>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>${junit.version}</version>
          <scope>test</scope>
        </dependency>
        <!--Lombok依赖-->
        <dependency>
          <groupId>org.projectlombok</groupId>
          <artifactId>lombok</artifactId>
          <version>1.16.18</version>
        </dependency>
        <!-- log4j日志依赖 -->
        <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-api</artifactId>
          <version>${slf4j.version}</version>
        </dependency>
        <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-log4j12</artifactId>
          <version>${slf4j.version}</version>
        </dependency>
        <dependency>
          <groupId>log4j</groupId>
          <artifactId>log4j</artifactId>
          <version>${log4j.version}</version>
        </dependency>
    
    
      </dependencies>
    
      <build>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
          <plugins>
            <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
            <plugin>
              <artifactId>maven-clean-plugin</artifactId>
              <version>3.1.0</version>
            </plugin>
            <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
            <plugin>
              <artifactId>maven-resources-plugin</artifactId>
              <version>3.0.2</version>
            </plugin>
            <plugin>
              <artifactId>maven-compiler-plugin</artifactId>
              <version>3.8.0</version>
            </plugin>
            <plugin>
              <artifactId>maven-surefire-plugin</artifactId>
              <version>2.22.1</version>
            </plugin>
            <plugin>
              <artifactId>maven-jar-plugin</artifactId>
              <version>3.0.2</version>
            </plugin>
            <plugin>
              <artifactId>maven-install-plugin</artifactId>
              <version>2.5.2</version>
            </plugin>
            <plugin>
              <artifactId>maven-deploy-plugin</artifactId>
              <version>2.8.2</version>
            </plugin>
            <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
            <plugin>
              <artifactId>maven-site-plugin</artifactId>
              <version>3.7.1</version>
            </plugin>
            <plugin>
              <artifactId>maven-project-info-reports-plugin</artifactId>
              <version>3.0.0</version>
            </plugin>
          </plugins>
        </pluginManagement>
      </build>
    </project>
    

    hello实例代码:
    Hello接口:

    package com.spring.Aop;
    
    /**
     * Created by User on 2019/3/7.
     */
    public interface Hello {
        String getHello();
    }
    

    实现类:

    package com.spring.Aop;
    
    /**
     * Created by User on 2019/3/7.
     */
    public class HelloImpl implements Hello{
        @Override
        public String getHello() {
            return "Hello,Spring Aop";
        }
    }
    

    MyBeforeAdvice:

    package com.spring.Aop;
    /**
     * Created by User on 2019/3/7.
     * 用户自定义的前置增强类
     */
    public class MyBeforeAdvice {
    //    定义前置方法
        public void beforeMethod(){
              System.out.println("This is a before method.");
        }
    
    }
    

    配置文件:

     <!--配置一个Hello的bean,等同于Hello hello=new HelloImpl();-->
        <bean id="hello" class="com.spring.Aop.HelloImpl"/>
        <!--配置一个MyBeforeAdvice前置增强的bean-->
        <bean id="myBeforeAdvice" class="com.spring.Aop.MyBeforeAdvice"/>
        <!--配置aop-->
        <aop:config>
            <aop:aspect id="before" ref="myBeforeAdvice">
                <aop:pointcut id="myPointCut" expression="execution(* com.spring.Aop.*.*(..))"/>
                <aop:before method="beforeMethod" pointcut-ref="myPointCut"/>
            </aop:aspect>
        </aop:config>
    

    HelloApp:

    package com.spring.Aop;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    /**
     * Created by User on 2019/3/7.
     */
    public class HelloAPP {
        public static void main(String[] args) {
            ApplicationContext context=new ClassPathXmlApplicationContext("Spring.xml");
            Hello hello=context.getBean(Hello.class);
            System.out.println(hello.getHello());
        }
    
    }
    
    结果.png

    相关文章

      网友评论

          本文标题:Spring Aop

          本文链接:https://www.haomeiwen.com/subject/xnuupqtx.html