10.Spring AOP

AOP (Aspect Oriented Programming)面向切面编程
OOP(Oriented Oriented Programming)面向对象编程
通过OOP的纵向和AOP的横向抽取,程序才可以真正解决问题
AOP的使用场景:日志 事务

AOP基本上是通过代理机制实现的

  • 单例模式
  • 代理
  • 工厂
  • 装饰器
  • 适配器
    代理的对象为接口

AOP中一些概念的理解

  • aop adviser:切面
  • aop aspect:切面配置
  • aop pointuct:切点
  • adviser=advice+pointuct即切面=增强+切点(在哪里做什么)

Hello前置增强练习

  • 给pom文件中添加aop依赖



  4.0.0

  com.spring
  AOP
  1.0-SNAPSHOT

  AOP
  
  http://www.example.com

  
    UTF-8
    1.8
    1.8
    5.0.10.RELEASE
    1.9.2
    4.12
    1.2.17
    1.7.12
  

  
    
      junit
      junit
      4.12
      test
    
    
    
      org.springframework
      spring-test
      ${spring.version}
    

    
    
      org.springframework
      spring-context
      ${spring.version}
    
    
    
      org.springframework
      spring-aop
      ${spring.version}
    
    
    
      org.aspectj
      aspectjrt
      ${aspectj.version}
    
    
      org.aspectj
      aspectjweaver
      ${aspectj.version}
    
    
    
      org.projectlombok
      lombok
      1.16.18
    
    
    
      org.slf4j
      slf4j-api
      1.6.6
    
    
      org.slf4j
      slf4j-log4j12
      1.6.6
    
  

  
    
      
        
        
          maven-clean-plugin
          3.1.0
        
        
        
          maven-resources-plugin
          3.0.2
        
        
          maven-compiler-plugin
          3.8.0
        
        
          maven-surefire-plugin
          2.22.1
        
        
          maven-jar-plugin
          3.0.2
        
        
          maven-install-plugin
          2.5.2
        
        
          maven-deploy-plugin
          2.8.2
        
        
        
          maven-site-plugin
          3.7.1
        
        
          maven-project-info-reports-plugin
          3.0.0
        
      
    
  

  • Hello接口及实现接口中方法的HelloImpl类
package com.spring;

public interface Hello {
    String getHello();
}

package com.spring;

public class HelloImpl implements Hello {
    @Override
    public String getHello() {
        return "Hello,Spring AOP";
    }
}
  • 用户自定义的前置增强类MyBeforeAdvicelei
package com.spring;

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 by wxy");
        logger.debug("This is a before method by wxy");


       // System.out.println("This is a before methoad");
    }

}
  • 配置文件

   
       
       
    
    
        
            
            
        
    
  • 主类
package com.spring;

import javafx.application.Application;
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());
    }

}
  • 运行结果

实战:模仿连接数据库的操作

  • UserDao接口
package com.spring;

public interface UserDao {
    void insert();
}


-UserDaoImpl类

package com.spring;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class UserDaoImpl implements UserDao {
    private static final Logger logger= LoggerFactory.getLogger(UserDaoImpl.class);
    @Override
    public void insert() {
        logger.info("开始插入一条数据。。。");
    }
}

  • MyBeforeAdvice前置增强类
package com.spring;

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 by wxy");
        logger.debug("This is a before method by wxy");


       // System.out.println("This is a before methoad");
    }

}

  • 配置文件

    
    
        
            
            
        
    
  • UserDao单元测试类
package com.spring;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import static org.junit.Assert.*;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/bean.xml"})
public class UserDaoTest {
    @Autowired
    private UserDao userDao;
    @Test
    public void insert(){
        userDao.insert();
    }

}
  • 运行结果


    Snipaste_2019-03-10_10-36-10.png

实战:使用@AspectJ的例子

  • 此例是演绎了一段“武松打虎”的情节——武松(Fighter)在山里等着老虎(Tiger)出现,只要发现老虎出来,就打老虎

1.定义业务类型

  • 老虎(Tiger)类
package com.spring;

public class Tiger {

    public void walk(){
        System.out.println("Tiger is walking...");
    }

}

2.定义切面和配置

  • 武松(Fighter)类
package com.spring;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class Fighter {
    @Pointcut("execution(* com.spring.Tiger.walk())")
    public void foundTiger(){

    }
    @Before(value = "foundTiger()")
    public void foundBefore(){
        System.out.println("Fighter wait for tiger...");
    }

    @AfterReturning("foundTiger()")
    public void foundAfter(){
        System.out.println("Fighter flight with tiger...");
    }
}

  • 相应的Spring配置

    
    
    
    

3.定义主应用

  • 主应用定义为
package com.spring;

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

public class Application {
    public static void main(String[] args) {
        @SuppressWarnings("resources")
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        Tiger tiger= context.getBean(Tiger.class);
        tiger.walk();
    }
}

4.运行

运行结果.png

你可能感兴趣的:(10.Spring AOP)