zoukankan      html  css  js  c++  java
  • SSM-Spring-03:Spring中AOP的初窥和入门小案例

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------

    AOP:面向切面编程

      AOP的主要作用:是为了程序员更好的关注"业务",专心"做事"

        加上双引号的意思:所谓业务,是指他的核心,各行业中需要处理的核心事务,核心啊

        像日志的记录,事务的管理,权限分配等这些交叉业务,同一个项目中使用多次,直接提取出来成为公共的比较好,再用面向切面的方式,进行代码的编辑,业务的实现

      AOP的思想:

        

        正如上图所示的,最基本的方式实现的业务和AOP方式实现的业务,是不同的,或许你会说,我在没有aop的时候,我也不像上面那个最low的方式一样啊,是,有比那个最low的方式好的多的是,我们要学习新的思想,aop,面向切面编程

      AOP的原理

        

    --------------------------------------------------------------------------------------------------------------------------------

    入门案例:

      用最基本的方式模拟一道日志的记录和最后执行完业务的操作

        DAO层(一个接口,一个他的实现类,模拟操作修改数据库)

    package cn.dawn.day04aop.dao;
    
    /**
     * Created by Dawn on 2018/3/5.
     */
    /*dao层接口*/
    public interface IHellowDAO {
        /*aop入门案例*/
        public void doSome();
    }
    
    
    package cn.dawn.day04aop.dao.impl;
    
    import cn.dawn.day04aop.dao.IHellowDAO;
    
    /**
     * Created by Dawn on 2018/3/5.
     */
    /*dao层实现类*/
    public class HellowDAOImpl implements IHellowDAO{
    
        public void doSome() {
            System.out.println("数据已经成功写入到DB");
        }
    }

        service层(也是一个接口,一个实现类,主要做的aop的增强操作,操作的是service层,业务逻辑处理层操作的)

    package cn.dawn.day04aop.service;
    
    /**
     * Created by Dawn on 2018/3/5.
     */
    /*service层接口*/
    public interface IHellowService {
        /*aop入门案例*/
        public void doSome();
    }
    
    
    
    
    package cn.dawn.day04aop.service.impl;
    
    import cn.dawn.day04aop.dao.IHellowDAO;
    import cn.dawn.day04aop.service.IHellowService;
    
    /**
     * Created by Dawn on 2018/3/5.
     */
    /*service层实现类 */
    public class HellowServiceImpl implements IHellowService {
        IHellowDAO dao;
        public void doSome() {
            dao.doSome();
        }
    
        public IHellowDAO getDao() {
            return dao;
        }
    
        public void setDao(IHellowDAO dao) {
            this.dao = dao;
        }
    }

        新开多的一层,叫aop层,他就存放了增强的操作(也就是交叉业务,例如日志记录等),此处我放了俩个类,一个执行前置增强,一个后置增强

    package cn.dawn.day04aop.aop;
    
    
    import org.springframework.aop.MethodBeforeAdvice;
    
    import java.lang.reflect.Method;
    
    /**
     * Created by Dawn on 2018/3/5.
     */
    /*前置增强*/
    public class LoggerBefore implements MethodBeforeAdvice {
        public void before(Method method, Object[] objects, Object o) throws Throwable {
            System.out.println("日志记录");
        }
    }
    
    
    
    
    
    
    
    package cn.dawn.day04aop.aop;
    
    import org.springframework.aop.AfterReturningAdvice;
    
    import java.lang.reflect.Method;
    
    /**
     * Created by Dawn on 2018/3/5.
     */
    /*后置增强*/
    public class LoggerAfter implements AfterReturningAdvice {
        public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
            System.out.println("===============after==================");
        }
    }

        前置增强,需要实现MethodBeforeAdvice,后置增强,需要实现AfterReturningAdvice

        接下来就是书写大配置xml文件

           有个注意的点,由于我用的idea,他会自动生成上面的beans的 xmlns和xsi,如果你不是用idea的话,手动配置一道吧

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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.xsd">
    
        <!--aop入门案例起-->
        <!--dao-->
        <bean id="dao" class="cn.dawn.day04aop.dao.impl.HellowDAOImpl"></bean>
        <!--service-->
        <bean id="service" class="cn.dawn.day04aop.service.impl.HellowServiceImpl">
            <property name="dao" ref="dao"></property>
        </bean>
        <!--通知-->
        <bean id="afterAdvice" class="cn.dawn.day04aop.aop.LoggerAfter"></bean>
        <bean id="beforeAdvice" class="cn.dawn.day04aop.aop.LoggerBefore"></bean>
        <!--aop-->
        <aop:config>
            <!--切点-->
            <aop:pointcut id="mypointcut" expression="execution(* *..service.*.*(..))"></aop:pointcut>
            <!--<aop:pointcut id="mypointcut" expression="execution(public void cn.dawn.day04aop.service.IHellowService.doSome())"></aop:pointcut>-->
            <!--<aop:pointcut id="mypointcut" expression="execution(* *..service.*.*(..))">-->
            <!--顾问,织入-->
            <aop:advisor advice-ref="beforeAdvice" pointcut-ref="mypointcut"></aop:advisor>
            <aop:advisor advice-ref="afterAdvice" pointcut-ref="mypointcut"></aop:advisor>
        </aop:config>
        <!--aop入门案例完毕-->
    </beans>

        这儿有一个切点pointcut,我说一下他的expression的属性吧,他就是里面放一个匹配的(可以说叫公式?)方法的公式

        他的使用规则我放在下面

                  

        接下来单测方法

    package cn.dawn.day04aop;
    
    import cn.dawn.day03printer.printer.Printer;
    import cn.dawn.day04aop.service.IHellowService;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    /**
     * Created by Dawn on 2018/3/3.
     */
    public class test20180305 {
        @Test
        /*aop入门案例*/
        public void t01(){
            ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day04aop.xml");
            IHellowService service = (IHellowService) context.getBean("service");
            service.doSome();
        }
    }

        运行结果如下:

  • 相关阅读:
    P2437 蜜蜂路线题解
    P1044 栈题解
    P1002 过河卒题解
    P1433 吃奶酪题解
    组合数公式
    P1036 选数题解
    十进制转二进制方法整理
    golang学习笔记 ---工作区与GOPATH
    golang学习笔记---闭包
    golang学习笔记---类型
  • 原文地址:https://www.cnblogs.com/DawnCHENXI/p/8510571.html
Copyright © 2011-2022 走看看