AOP

作者: 六年的承诺 | 来源:发表于2019-03-09 22:51 被阅读0次

    反射

    java创建对象的方法(5种):

    Spring AOP:

    spring分为:1、IOC/DI 2、AOP
    AOP的使用场景:日志和事务
    概念:AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。

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

    Move类

    package com.spring;

    public interface Move {
    void move();
    }

    Car类

    package com.spring;

    public class Car implements Move {
    @Override
    public void move() {
    System.out.println("Car is moving...");
    }
    }

    Tank类

    package com.spring;

    public class Tank implements Move {
    @Override
    public void move() {
    System.out.println("Tank is moving...");
    }
    }

    TankProxy类

    package com.spring;

    public class TankProxy implements Move {
    private Move t;

    public TankProxy(Move t) {
        this.t = t;
    }
    
    @Override
    public void move() {
        System.out.println("start");
        t.move();
        System.out.println("stop");
    }
    

    }

    代理不能代理具体的类,得代理接口

    MoveApp类

    package com.spring;

    public class MoveApp {
    public static void main(String[] args) {
    Move t1=new Tank();
    Move t2=new Car();
    Move moveProxy = new TankProxy(t2);
    moveProxy.move();
    }
    }

    运行结果

    moveAPP.png

    在pom.xml中添加了依赖(加上之前的总共五个)

    <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>
    <s1f4j.version>1.7.12</s1f4j.version>
    </properties>


    <dependencies>
    <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-context依赖-->
    <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>
    
    <!-- <spring-test依赖>-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>${spring.version}</version>
    </dependency>
    
    <!-- log4j日志依赖 -->
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.6.6</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>1.6.6</version>
    </dependency>
    
      <!--junit测试-->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    

    </dependencies>

    Hello的前置增强练习

    Hello类

    package com.spring;

    public interface Hello {
    String getHello();
    }

    HelloImpl

    package com.spring;

    public class HelloImpl implements Hello {
    @Override
    public String getHello() {
    return "Hello,Spring AOP";
    }
    }

    MyBeforeAdvice

    package com.spring;

    import org.aspectj.lang.JoinPoint;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;

    /*

    • 用户自定义的前置增强类
    • /
      public class MyBeforeAdvice {
      private static final Logger logger= LoggerFactory.getLogger(MyBeforeAdvice.class);
      /
      定义前置方法*/
      public void beforeMethod() {
      logger.info("This is a before method ");
      logger.debug("This is a before method ");
       // System.out.println("This is a before methoad");
    }
    

    }

    配置文件

    <bean id="hello" class="com.spring.HelloImpl"/>

    <bean id="mybeforeAdvice" class="com.spring.MyBeforeAdvice"/>

    <aop:config>
    <aop:aspect id="before" ref="mybeforeAdvice">
    <aop:pointcut id="myPointcut" expression="execution(* com.spring..(..))"/>
    <aop:before method="beforeMethod" pointcut-ref="myPointcut"/>

        </aop:aspect>
    </aop:config>
    

    HelloApp类

    package com.spring;

    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;

    public class HelloApp {
    public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext("/bean.xml");
    Hello hello = context .getBean(Hello.class);
    System.out.println(hello.getHello());
    }

    }

    运行结果

    hello.png

    相关文章

      网友评论

          本文标题:AOP

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