美文网首页
day05-02 AOP学习

day05-02 AOP学习

作者: 黑桃_06ea | 来源:发表于2019-03-10 19:54 被阅读0次

1、了解AOP

AOP(Aspect Oriented Programming,面向切面编程),通过提供另一种思考程序的方式来补充OOP(Object Oriented Programming,面向对象编程)。AOP是横向抽取,OOP是纵向抽象。
切面可以用于事务管理、日志等方面的模块化

  • AOP关键词:
    Aspect(切面),Join Point(连接点),Advice(通知/增强),Pointcut(切点),Introduction(引入),Target Object(目标对象),AOP Proxy(AOP代理),Weaving(织入)
  • Advice的主要类型有:
    Before Advice(前置通知),After Returning Advice(返回后通知),After Throwing Advice(抛出异常后通知),After (finally)Advice(最后通知),Around Advice(环绕通知)(切入点和连接点的匹配,是AOP的关键)

2、Hello前置增强练习

  • 添加依赖
<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>
    <!--spring-context依赖-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>${aspectj.version}</version>
    </dependency>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjrt</artifactId>
      <version>${aspectj.version}</version>
    </dependency>
    <!--spring-test依赖-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <!--junit依赖-->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>${junit.version}</version>
      <scope>test</scope>
    </dependency>
    <!-- log4j日志依赖 -->
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>${log4j.version}</version>
    </dependency>
    <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>
</dependencies>
  • Hello相关类
public interface Hello {
    String getHello();
}
public class HelloImpl implements Hello {
    @Override
    public String getHello() {
        return "Hello,Spring AOP";
    }
}
/**
 * 用户自定义的前置增强类
 */
public class MyBeforeAdvice {
//    定义前置方法
    public void beforeMethod(){
        System.out.println("this is a before method");
    }
}
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class HelloApp {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Hello hello = context.getBean(Hello.class);
        System.out.println(hello.getHello());
    }
}
  • 配置bean
<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>
  • 运行结果


    day05-01.png

3、Tank练习

  • 相关类
public class Tank implements Move {
    @Override
    public void move() {
        System.out.println("Tank is moving");
    }
}
public class Car implements Move {
    @Override
    public void move() {
        System.out.println("Car is moving...");
    }
}
public interface Move {
    public void move();
}
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");
    }
}
public class MoveApp {
    public static void main(String[] args) {
        Move t=new Tank();
        Move t1=new Car();
        Move moveproxy=new TankProxy(t);
        Move moveproxy1=new TankProxy(t1);
        moveproxy.move();
        moveproxy1.move();
    }
}
  • 运行结果


    day05-02.png

相关文章

网友评论

      本文标题:day05-02 AOP学习

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