spring第三天

    一、AOP的相关概念

        1.1AOP概述

            1.1.1什么是AOP

                AOP:全称是Aspext Oriented Programming即:面向切面编程。

简单的说它就是我们程序重复的代码抽取出来,在需要执行的时候,使用动态代理的技术,在不修改源码的基础上,对我们的已有方法进行增强。

1.1.2AOP的作用及优势

                作用:

                    在程序运行期间,不修改源码对已有方法进行增强。

                优势:

                    减少重复代码        提高开发效率        维护方便

1.1.3AOP的实现方式

                使用动态代理技术

1.2AOP的具体应用

            1.2.1案例中问题

                这是我们之前在struts2课程中做的一个完整的增删改查例子。下面是客户的业务层接口和实现类。

                    客户的业务层接口

                    /**

                     * 客户的业务层接口

                     */

                    public interface ICustomerService {

                        

                        /**

                         * 保存客户

                         * @param customer

                         */

                        void saveCustomer(Customer customer);

                        

                        /**

                         * 查询所有客户

                         * @return

                         */

                        List<Customer> findAllCustomer();

                        

                        /**

                         * 删除客户

                         * @param customer

                         */

                        void removeCustomer(Customer customer);

                        

                        /**

                         * 根据id查询客户

                         * @param custId

                         * @return

                         */

                        Customer findCustomerById(Long custId);

                        

                        /**

                         * 修改客户

                         * @param customer

                         */

                        void updateCustomer(Customer customer);

                    }

客户的业务层实现类

                    /**

                     * 客户的业务层实现类

                     * 事务必须在此控制

                     * 业务层都是调用持久层的方法

                     */

                    public class CustomerServiceImpl implements ICustomerService {

private ICustomerDao customerDao = new CustomerDaoImpl();

                            

                        @Override

                        public void saveCustomer(Customer customer) {

                            Session s = null;

                            Transaction tx = null;

                            try{

                                s = HibernateUtil.getCurrentSession();

                                tx = s.beginTransaction();

                                customerDao.saveCustomer(customer);

                                tx.commit();

                            }catch(Exception e){

                                tx.rollback();

                                throw new RuntimeException(e);

                            }

                        }

@Override

                        public List<Customer> findAllCustomer() {

                            Session s = null;

                            Transaction tx = null;

                            try{

                                s = HibernateUtil.getCurrentSession();

                                tx = s.beginTransaction();

                                List<Customer> customers = customerDao.findAllCustomer();

                                tx.commit();

                                return customers;

                            }catch(Exception e){

                                tx.rollback();

                                throw new RuntimeException(e);

                            }

                        }

@Override

                        public void removeCustomer(Customer customer) {

                            Session s = null;

                            Transaction tx = null;

                            try{

                                s = HibernateUtil.getCurrentSession();

                                tx = s.beginTransaction();

                                customerDao.removeCustomer(customer);

                                tx.commit();

                            }catch(Exception e){

                                tx.rollback();

                                throw new RuntimeException(e);

                            }

                        }

@Override

                        public Customer findCustomerById(Long custId) {

                            Session s = null;

                            Transaction tx = null;

                            try{

                                s = HibernateUtil.getCurrentSession();

                                tx = s.beginTransaction();

                                Customer c = customerDao.findCustomerById(custId);

                                tx.commit();

                                return c;

                            }catch(Exception e){

                                tx.rollback();

                                throw new RuntimeException(e);

                            }

                        }

@Override

                        public void updateCustomer(Customer customer) {

                            Session s = null;

                            Transaction tx = null;

                            try{

                                s = HibernateUtil.getCurrentSession();

                                tx = s.beginTransaction();

                                customerDao.updateCustomer(customer);

                                tx.commit();

                            }catch(Exception e){

                                tx.rollback();

                                throw new RuntimeException(e);

                            }

                        }

                    }

1.2.2动态代理回顾

                1.2.2.1动态代理的特点

                    字节码随用随创建,随用随加载。

                    它与静态代理的区别也在于此。因为静态代理是字节码一上来就创建好,并完成加载。

                    装饰者模式就是静态代理的一种体现。

1.2.2.2动态代理常用的有两种方式

                    基于接口的动态代理

                        提供者:JDK官方的Porxy类

                        要求:被代理者最少实现一个接口

基于子类的动态代理

                        提供者:第三方的CGLib,如果报asmxxxx异常,需要导入asm.jar。

                        要求:被代理类不能用final修饰的类(最终类)

1.2.2.3使用JDK官方的Proxy类创建代理对象

                    此时就是一个演员的例子:

                        很久以前,演员和剧组是直接联系见面的,没有中间人环节。

                        而随着时间的推移,产生了一个新兴行业:经纪人(中间人),这个时候剧组再想找演员就需要通过经纪人来找了。下面我们就用代码演示出来。

                            /**

                             * 一个经纪公司的要求:

                             *         能做基本的表演和危险的表演

                            */

                            public interface IActor {

                                /**

                                 * 基本演出

                                 * @param money

                                 */

                                public void basicAct(float money);

                                /**

                                 * 危险演出

                                 * @param money

                                 */

                                public void dangerAct(float money);

                            }

/**

                             * 一个演员

                             */

                            //实现了接口,就表示具有接口中的方法实现。即:符合经纪公司的要求

                            public class Actor implements IActor{

                                

                                public void basicAct(float money){

                                    System.out.println("拿到钱,开始基本的表演:"+money);

                                }

                                

                                public void dangerAct(float money){

                                    System.out.println("拿到钱,开始危险的表演:"+money);

                                }

                            }

public class Client {

                                

                                public static void main(String[] args) {

                                    //一个剧组找演员:

                                    final Actor actor = new Actor();//直接

                                    

                                    /**

                                     * 代理:

                                     *     间接。

                                     * 获取代理对象:

                                     *     要求:

                                     *      被代理类最少实现一个接口

                                     * 创建的方式

                                     *   Proxy.newProxyInstance(三个参数)

                                     * 参数含义:

                                     *     ClassLoader:和被代理对象使用相同的类加载器。

                                     *  Interfaces:和被代理对象具有相同的行为。实现相同的接口。

                                     *  InvocationHandler:如何代理。

                                     *          策略模式:使用场景是:

                                     *                      数据有了,目的明确。

                                     *                      如何达成目标,就是策略。

                                     *              

                                     */

                                    IActor proxyActor = (IActor) Proxy.newProxyInstance(

                                                                    actor.getClass().getClassLoader(), 

                                                                    actor.getClass().getInterfaces(), 

                                                                    new InvocationHandler() {

                                            /**

                                             * 执行被代理对象的任何方法,都会经过该方法。

                                             * 此方法有拦截的功能。

                                             * 

                                             * 参数:

                                             *     proxy:代理对象的引用。不一定每次都用得到

                                             *     method:当前执行的方法对象

                                             *     args:执行方法所需的参数

                                             * 返回值:

                                             *     当前执行方法的返回值

                                             */

                                            @Override

                                            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

                                                String name = method.getName();

                                                Float money = (Float) args[0];

                                                Object rtValue = null;

                                                //每个经纪公司对不同演出收费不一样,此处开始判断

                                                if("basicAct".equals(name)){

                                                    //基本演出,没有2000不演

                                                    if(money > 2000){

                                                        //看上去剧组是给了8000,实际到演员手里只有4000

                                                        //这就是我们没有修改原来basicAct方法源码,对方法进行了增强

                                                        rtValue = method.invoke(actor, money/2);

                                                    }

                                                }

                                                if("dangerAct".equals(name)){

                                                    //危险演出,没有5000不演

                                                    if(money > 5000){

                                                        //看上去剧组是给了50000,实际到演员手里只有25000

                                                        //这就是我们没有修改原来dangerAct方法源码,对方法进行了增强

                                                        rtValue = method.invoke(actor, money/2);

                                                    }

                                                }

                                                return rtValue;

                                            }

                                    });

                                    //没有经纪公司的时候,直接找演员。

                            //        actor.basicAct(1000f);

                            //        actor.dangerAct(5000f);

                                    

                                    //剧组无法直接联系演员,而是由经纪公司找的演员

                                    proxyActor.basicAct(8000f);

                                    proxyActor.dangerAct(50000f);

                                }

                            }

1.2.2.4使用CGLib的Enhancer类创建代理对象

                    还是那个演员的例子,只不过不让他实现接口

                        /**

                         * 一个演员

                        */

                        public class Actor{//没有实现任何接口

                            

                            public void basicAct(float money){

                                System.out.println("拿到钱,开始基本的表演:"+money);

                            }

                            

                            public void dangerAct(float money){

                                System.out.println("拿到钱,开始危险的表演:"+money);

                            }

                        }

public class Client {

                            /**

                             * 基于子类的动态代理

                             *     要求:

                             *         被代理对象不能是最终类

                             *     用到的类:

                             *         Enhancer

                             *     用到的方法:

                             *         create(Class, Callback)

                             *     方法的参数:

                             *         Class:被代理对象的字节码

                             *         Callback:如何代理

                             * @param args

                             */

                            public static void  main(String[] args) {

                                final Actor actor = new Actor();

                                

                                Actor cglibActor = (Actor) Enhancer.create(actor.getClass(),

                                                    new MethodInterceptor() {

                                    /**

                                     * 执行被代理对象的任何方法,都会经过该方法。在此方法内部就可以对被代理对象的任何方法进行增强。

                                     * 

                                     * 参数:

                                     *     前三个和基于接口的动态代理是一样的。

                                     *     MethodProxy:当前执行方法的代理对象。

                                     * 返回值:

                                     *     当前执行方法的返回值

                                     */

                                    @Override

                                    public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {

                                        String name = method.getName();

                                        Float money = (Float) args[0];

                                        Object rtValue = null;

                                        if("basicAct".equals(name)){

                                            //基本演出

                                            if(money > 2000){

                                                rtValue = method.invoke(actor, money/2);

                                            }

                                        }

                                        if("dangerAct".equals(name)){

                                            //危险演出

                                            if(money > 5000){

                                                rtValue = method.invoke(actor, money/2);

                                            }

                                        }

                                        return rtValue;

                                    }

                                });        

                                cglibActor.basicAct(10000);

                                cglibActor.dangerAct(100000);

                            }

                        }

1.2.3解决案例中的问题

                思路只有一个:使用动态代理技术创建客户业务层的代理对象,在执行CustomerServiceImpl时,对里面的方法进行增强,加入事务的支持。

                    /**

                     * 用于创建客户业务层对象工厂(当然也可以创建其他业务层对象,只不过我们此处不做那么繁琐)

                    */

                    public class BeanFactory {

                        

                        /**

                         * 获取客户业务层对象的代理对象

                         * @return

                         */

                        public static ICustomerService getCustomerService() {

                            //定义客户业务层对象

                            final ICustomerService customerService = new CustomerServiceImpl();

                            //生成它的代理对象

                            ICustomerService proxyCustomerService = (ICustomerService) 

                                Proxy.newProxyInstance(customerService.getClass().getClassLoader()

                                ,customerService.getClass().getInterfaces(), 

                                new InvocationHandler() {

                                //执行客户业务层任何方法,都会在此处被拦截,我们对那些方法增强,加入事务。    

                                @Override

                                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

                                    String name = method.getName();

                                    Object rtValue = null;

                                    try{

                                        //开启事务

                                        HibernateUtil.beginTransaction();                    

                                        //执行操作

                                        rtValue = method.invoke(customerService, args);            

                                        //提交事务

                                        HibernateUtil.commit();

                                    }catch(Exception e){

                                        //回滚事务

                                        HibernateUtil.rollback();    

                                        e.printStackTrace();

                                    }finally{

                                        //释放资源.hibernate在我们事务操作(提交/回滚)之后,已经帮我们关了。

                                        //如果他没关,我们在此处关

                                    }

                                    return rtValue;

                                }

                            });

                            return proxyCustomerService;

                        }

                    }

1.3Spring中AOP

            1.3.1关于代理的选择

                在spring中,框架会根据目标类是否实现了接口来决定采用哪种动态代理的方式。

1.3.2AOP相关术语

                Joinpoint(连接点):

                    所谓连接点是指的那些被拦截到的点。在spring中,这些点指的是方法,因为spring只支持方法类型的连接点。

Pointcut(切入点):

                    所谓切入点是指我们要对哪些Joinpoint进行拦截的定义。

Advice(通知/增强)

                    所谓通知是指拦截到Joinpoint之后所要做的事情就是通知。

                    通知的类型:前置通知,后置通知,异常通知,最终通知,环绕通知。

Introduction(引介):

                    引介是一种特殊的通知在不修改类代码的前提下,Introduction可以在运行期为类动态地添加一些方法或Field。

Target(目标对象):

                    代理的目标对象。

Weaving(织入):

                    是指把增强应用到目标对象来创建新的代理对象的过程。

                    spring采用动态代理织入,而AspectJ采用编译器织入和类装载器织入。

Proxy(代理):

                    一个类被AOP织入增强后,就产生了一个结果代理类。

Aspect(切面):

                    是切入点和通知(引介)的结合。

1.3.3学习spring中的AOP要明确的事。

                a、开发阶段(我们做的)

                    编写核心业务代码(开发主线):大部分程序员来做,要求熟悉业务需求。

                    把公用代码抽取出来,制作成通知。(开发阶段最后来做):AOP编程人员来做。

                    在配置文件中,声明切入点与通知间的关系,即切面。:AOP编程人员来做。

b、运行阶段(Spring框架完成的)

                    Spring框架监控切入点方法的执行。一旦监控到切入点方法被运行,使用代理机制,动态创建目标对象的代理对象,根据通知类别,在代理对象的对应位置,将通知对应的功能织入,完成完整的代码逻辑运行。

二、基于XML的AOP配置

        2.1环境搭建

            2.1.1第一步:准备客户的业务层和接口(需要增强的类)

                /**

                 * 客户的业务层接口

                */

                public interface ICustomerService {

                    

                    /**

                     * 保存客户

                     */

                    void saveCustomer();

                    

                    /**

                     * 修改客户

                     * @param i

                     */

                    void updateCustomer(int i);

                }

/**

                 * 客户的业务层实现类

                 */

                public class CustomerServiceImpl implements ICustomerService {

@Override

                    public void saveCustomer() {

                        System.out.println("调用持久层,执行保存客户");

                    }

@Override

                    public void updateCustomer(int i) {

                        System.out.println("调用持久层,执行修改客户");

                    }

                }

2.1.2第二步:拷贝必备的jar包到工厂的lib目录

2.1.3第三步:创建spring的配置文件并导入约束

                <?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">

</beans>

2.1.4第四步:把客户的业务层配置到spring容器中

                <!-- 把资源交给spring来管理 -->

                <bean id="customerService" class="com.itheima.service.impl.CustomerServiceImpl"/>

2.1.5第五步:制作通知(增强的类)

                /**

                 * 一个记录日志的工具类

                */

                public class Logger {

                    /**

                     * 期望:此方法在业务核心方法执行之前,就记录日志

                     */

                    public void beforePrintLog(){

                        System.out.println("Logger类中的printLog方法开始记录日志了。。。。");

                    }

                }

2.2配置步骤

            2.2.1第一步:把通知类用bean标签配置起来

                <!-- 把有公共代码的类也让spring来管理(把通知类也交给spring来管理) -->

                <bean id="logger" class="com.itheima.util.Logger"></bean>

2.2.2第二步:使用aop:config声明aop配置

                <!-- aop的配置 -->

                <aop:config>

                    <!-- 配置的代码都写在此处 -->    

                </aop:config>

2.2.3第三步:使用aop:aspect配置切面

                <!-- 配置切面 :此标签要出现在aop:config内部

                    id:给切面提供一个唯一标识

                    ref:引用的是通知类的bean的id

                -->

                <aop:aspect id="logAdvice" ref="logger">

                        <!--配置通知的类型要写在此处-->

                </aop:aspect>

2.2.4第四步:使用aop:begore配置前置通知

                <!-- 用于配置前置通知:指定增强的方法在切入点方法之前执行 

                        method:用于指定通知类中的增强方法名称

                        ponitcut-ref:用于指定切入点的表达式的引用    

                -->

                <aop:before method="beforePrintLog" pointcut-ref="pt1"/>

2.2.5第五步:使用aop:pointcut配置切入点表达式

                <aop:pointcut expression="execution(public void com.changemax.service.impl.CustomerServiceImpl.saveCustomer())" 

                id="pt1"/>

2.3切入点表达式说明:

            execution:

                    匹配方法的执行(常用)        

                    execution(表达式)

            表达式语法:execution([修饰符] 返回值类型 包名.类名.方法名(参数))

            写法说明:

                全匹配方式:

                    public void com.itheima.service.impl.CustomerServiceImpl.saveCustomer()

访问修饰符可以省略 void 

                    com.itheima.service.impl.CustomerServiceImpl.saveCustomer()

返回值可以使用*号,表示任意返回值

                    * com.itheima.service.impl.CustomerServiceImpl.saveCustomer()

包名可以使用*号,表示任意包,但是有几级包,需要写几个*

                    * *.*.*.*.CustomerServiceImpl.saveCustomer()

使用..来表示当前包,及其子包

                    * com..CustomerServiceImpl.saveCustomer()

类名可以使用*号,表示任意类

                    * com..*.saveCustomer()

方法名可以使用*号,表示任意方法

                    * com..*.*()

参数列表可以使用*,表示参数可以是任意数据类型,但是必须有参数

                    * com..*.*(*)

参数列表可以使用..表示有无参数均可,有参数可以是任意类型

                    * com..*.*(..)

全通配方式:

                    * *..*.*(..)

2.4常用标签

            2.4.1<aop:config>

                作用:

                    用于声明开始aop的配置

2.4.2<aop:aspect>

                作用:

                    用于配置切面

                属性:

                    id:给切面提供一个唯一标识

                    ref:引用配置好的通知类bean的id。

2.4.3<aop:pointcut>

                作用:

                    用于配置切入点表达式

                属性:

                    expression:用于定义切入点表达式

                    id:用于给切入点表达式提供一个唯一标识。

2.4.4<aop:before>

                作用:

                    用于配置前置通知

                属性:

                    method:指定通知中方法的名称。

                    pointct:定义切入点表达式。

                    pointcut-ref:指定切入点表达式的引用

2.4.5<aop:after-returning>

                作用:

                    用于配置后置通知

                属性:

                    method:指定通知中方法的名称。

                    pointct:定义切入点表达式。

                    pointcut-ref:指定切入点表达式的引用

2.4.6<aop:after-throwing>

                作用:

                    用于配置异常通知

                属性:

                    method:指定通知中方法的名称。

                    pointct:定义切入点表达式。

                    pointcut-ref:指定切入点表达式的引用

2.4.7<aop:after>

                作用:

                    用于配置最终通知

                属性:

                    method:指定通知中方法的名称。

                    pointct:定义切入点表达式。

                    pointcut-ref:指定切入点表达式的引用

2.4.8<aop:around>

                作用:

                    用于配置环绕通知

                属性:

                    method:指定通知中方法的名称。

                    pointct:定义切入点表达式。

                    pointcut-ref:指定切入点表达式的引用

2.5通知的类型

            2.5.1类型说明

                <!-- 配置通知的类型

                    aop:before:

                        用于配置前置通知。前置通知的执行时间点:切入点方法执行之前执行

aop:after-returning:

                        用于配置后置通知。后置通知的执行时间点:切入点正常执行之后。它和异常通知只能有一个执行。

aop:after-throwing

                        用于配置异常通知。异常通知的执行时间点:切入点方法执行产生异常后执行。它和后置通知只能执行一个。

aop:after

                        用于配置最终配置。最终通知的执行时间点:无论切入带你方法执行时是否异常,它都会在其后面执行。

aop:around

                        用于配置环绕通知。他和前面四个不一样,他不是用于指定通知方法何时执行的。

                -->

                <aop:before method="beforePrintLog" pointcut-ref="pt1"/>

                <aop:after-returning method="afterReturningPrintLog"  pointcut-ref="pt1"/>

                <aop:after-throwing method="afterThrowingPrintLog" pointcut-ref="pt1"/>

                <aop:after method="afterPrintLog" pointcut-ref="pt1"/>

                <aop:around method="aroundPringLog" pointcut-ref="pt1"/>

2.5.2环绕通知的特殊说明:

                /**

                 * 环绕通知

                 *     它是spring框架为我们提供的一种可以在代码中手动控制增强部分什么时候执行的方式。

                 * 问题:

                 *     当我们配置了环绕通知之后,增强的代码执行了,业务核心方法没有执行。

                 * 分析:

                 *     通过动态代理我们知道在invoke方法中,有明确调用业务核心方法:method.invoke()。

                 *     我们配置的环绕通知中,没有明确调用业务核心方法。

                 * 解决:

                 *     spring框架为我们提供了一个接口:ProceedingJoinPoint,它可以作为环绕通知的方法参数

                 *     在环绕通知执行时,spring框架会为我们提供该接口的实现类对象,我们直接使用就行。

                 *     该接口中有一个方法proceed(),此方法就相当于method.invoke()

                 */

                 public void aroundPringLog(ProceedingJoinPoint pjp){

                     try {

                        System.out.println("前置通知:Logger类的aroundPringLog方法记录日志");

                        pjp.proceed();

                        System.out.println("后置通知:Logger类的aroundPringLog方法记录日志");

                    } catch (Throwable e) {

                        System.out.println("异常通知:Logger类的aroundPringLog方法记录日志");

                        e.printStackTrace();

                    }finally{

                        System.out.println("最终通知:Logger类的aroundPringLog方法记录日志");

                    }

}

三、基于注解的AOP配置

        3.1环境搭建

            3.1.1第一步:准备客户的业务层和接口并用注解配置(需要增强的类)

                /**

                 * 客户的业务层接口

                */

                public interface ICustomerService {

                    

                    /**

                     * 保存客户

                     */

                    void saveCustomer();

                    

                    /**

                     * 修改客户

                     * @param i

                     */

                    void updateCustomer(int i);

                }

/**

                 * 客户的业务层实现类

                 */

                public class CustomerServiceImpl implements ICustomerService {

@Override

                    public void saveCustomer() {

                        System.out.println("调用持久层,执行保存客户");

                    }

@Override

                    public void updateCustomer(int i) {

                        System.out.println("调用持久层,执行修改客户");

                    }

                }

3.1.2第二步;拷贝必备的jar包到工程的lib目录

3.1.3第三步:创建spring的配置文件并导入约束

                <?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"

                        xmlns:context="http://www.springframework.org/schema/context"

                        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

                                http://www.springframework.org/schema/context 

                                http://www.springframework.org/schema/context/spring-context.xsd">

                  

                </beans>

3.1.4第四步:把资源使用注解让spring来管理

                /**

                 * 客户的业务层实现类

                 */

                @Service("customerService")

                public class CustomerServiceImpl implements ICustomerService {

@Override

                    public void saveCustomer() {

                        System.out.println("调用持久层,执行保存客户");

                    }

                    @Override

                    public void updateCustomer(int i) {

                        System.out.println("调用持久层,执行修改客户");

                    }

                }

3.1.5第五步:在配置文件中指定spring要扫描的包

                <!-- 告知spring,在创建容器时要扫描的包 -->

                <context:component-scan base-package="com.itheima"></context:component-scan>

3.2配置步骤

            3.2.1第一步:把通知类也使用注解配置

                /**

                 * 一个记录日志的工具类

                 */

                @Component("logger")

                public class Logger {

                    /**

                     * 期望:此方法在业务核心方法执行之前,就记录日志

                     * 前置通知

                     */

                    public void beforePrintLog(){

                        System.out.println("前置通知:Logger类中的printLog方法开始记录日志了");

                    }

                }

3.2.2第二步:在通知类上使用@Aspect注解声明为切面

                /**

                 * 一个记录日志的工具类

                 */

                @Component("logger")

                @Aspect//表明当前类是一个切面类

                public class Logger {

                    /**

                     * 期望:此方法在业务核心方法执行之前,就记录日志

                     * 前置通知

                     */

                    public void beforePrintLog(){

                        System.out.println("前置通知:Logger类中的printLog方法开始记录日志了");

                    }

                }

3.2.3第三步:在增强的方法上使用@Before注解配置前置通知

                /**

                 * 期望:此方法在业务核心方法执行之前,就记录日志

                 * 前置通知

                 */

                @Before("execution(* com.itheima.service.impl.*.*(..))")//表示前置通知

                public void beforePrintLog(){

                    System.out.println("前置通知:Logger类中的printLog方法开始记录日志了");

                }

3.2.4第四步:在spring配置文件中开启spring对注解aop的支持

                <!-- 开启spring对注解AOP的支持 -->

                <aop:aspectj-autoproxy/>

3.3常用注解

            3.3.1@Aspect:

                作用:

                    把当前类声明为切面类。

3.3.2@Before:

                作用:

                    把当前方法看成是前置通知。

                属性:

                    value:用于指定切入点表达式,还可以指定切入点表达式的引用。

3.3.3@AfteReturning

                作用:

                    把当前方法看成是异常通知。

                属性:

                    value:用于指定切入点表达式,还可以指定切入点表达式的引用。

3.3.4@AfterThrowing

                作用:

                    把当前方法看成异常通知。

                属性:

                    value:用于指定切入点表达式,还可以指定切入点表达式的引用。

3.3.5@After

                作用:

                    把当前方法看成是最终通知

                属性:

                    value:用于指定切入点表达式,还可以指定切入点表达式的引用。

3.3.6@Around

                作用:

                    把当前方法看成是环绕通知

                属性:

                    value:用于指定切入点表达式,还可以指定切入点表达式的引用。

3.3.7@Pointcut

                作用:

                    指定切入点表达式

                属性:

                    value:指定表达式的内容

3.4不适用xml的配置方式

            @Configuration

            @ComponentScan(basePackages="com.changemax")

            @EnableAspectJAutoProxy

            public class SpringConfiguration{

            

            }

开放源代码的设计层面框架Spring——day03的更多相关文章

  1. 开放源代码的设计层面框架Spring——day01

    spring第一天     一.Spring概述         1.1spring概述             1.1.1spring介绍                 Spring是分层的Jav ...

  2. 开放源代码的设计层面框架Spring——day04

    spring第四天     一.Spirng中的JdbcTemplate         1.1JbdcTemplate概述             他是spring框架中提供的一个对象,是对原始Jd ...

  3. 开放源代码的设计层面框架Spring——day02

    spring第二天     一.基于注解的IOC配置         1.1写在最前             学习基于注解的IOC配置,大家脑海里首先得有一个认知,即注解配置和xml配置要实现的功能是 ...

  4. Hibernate(开放源代码的对象关系映射框架)

    Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系,是一个全自动的orm框架,hibernate可以自动生成SQL语句,自 ...

  5. JavaEE之Hibernate(开放源代码的对象关系映射框架)

    Hibernate(开放源代码的对象关系映射框架) 1.简介 Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系,是一个全 ...

  6. [JSP]Maven+SSM框架(Spring+SpringMVC+MyBatis) - Hello World

    来源:http://blog.csdn.net/zhshulin/article/details/37956105?utm_source=tuicool&utm_medium=referral ...

  7. 开源倾情奉献:基于.NET打造IP智能网络视频监控系统(一)开放源代码

    本文为 Dennis Gao 原创技术文章,发表于博客园博客,未经作者本人允许禁止任何形式的转载. 开源倾情奉献系列链接 开源倾情奉献:基于.NET打造IP智能网络视频监控系统(一)开放源代码 开源倾 ...

  8. Maven+SSM框架(Spring+SpringMVC+MyBatis) - Hello World(转发)

    [JSP]Maven+SSM框架(Spring+SpringMVC+MyBatis) - Hello World 来源:http://blog.csdn.net/zhshulin/article/de ...

  9. 第二个基础框架 — spring — xml版,没用注解 — 更新完毕

    1.什么是spring? 老规矩:百度百科一手 这上面说得太多了,我来提炼一下: spring就是一个轻量级的控制反转( IOC ) 和 面向切面编程( AOP ) 的容量框架.总的来说:本质就是对j ...

随机推荐

  1. Gitlab源码库里代码提交后,如何触发jenkins自动构建?

    版本库里代码提交后,如何触发jenkins自动构建?这是一个面试题,感觉自己回答的并不好,因为并没有用过这个功能,之前公司实际项目用的是svn版本管理,一般都用立刻构建,和定时任务构建(不管代码是否有 ...

  2. IBM developer:Setting up the Kafka plugin for Ranger

    Follow these steps to enable and configure the Kafka plugin for Ranger. Before you begin The default ...

  3. 19 款仿 Bootstrap 后台管理主题免费下载

    声明: 1. 本篇文章提到的仿 Bootstrap 风格的主题,是基于 jQuery 的 ASP.NET MVC 控件库的主题. 2. FineUIMvc(基础版)完全免费,可以用于商业项目. 目录 ...

  4. 关于vue build时一直报错

    真鸡儿坑,截图说下是什么错: 像上面这种,一大堆,看不出具体是为什么,然后根据网上搜到的教程注释了webpack.base.conf.js里的某一行: 重新build,成功.......... 醉了啊 ...

  5. Kafka简介及使用PHP处理Kafka消息

    Kafka简介及使用PHP处理Kafka消息 Kafka 是一种高吞吐的分布式消息系统,能够替代传统的消息队列用于解耦合数据处理,缓存未处理消息等,同时具有更高的吞吐率,支持分区.多副本.冗余,因此被 ...

  6. CentOS6.5-安装yum方法

    1.卸载原有系统中的yumrpm –qa | grep yum | xargs rpm -e --nodeps (xargs 把rpm –qa | grep yum传来的每一行信息 都用xargs后面 ...

  7. 【zabbix教程系列】三、zabbix 3.4 在centos 7 上安装详细步骤

    一.环境准备 [root@ltt01 ~]# ip a : lo: <LOOPBACK,UP,LOWER_UP> mtu qdisc noqueue state UNKNOWN qlen ...

  8. MRP没生成MRP汇总表

    设置:工作日历延长

  9. iptables对端口流量统计

    本篇文章主要介绍了Linux下如何对端口流量进行统计,小编觉得挺不错的,现在分享给大家,也给大家做个参考.一起跟随小编过来看看吧   在不修改源代码的情况下对程序暴露端口流量进行监控统计,可以利用Li ...

  10. es6中常用方法

    查询数组中是否包含了某个元素keyword arr.includes(keyword)