十一.mian向切面编程---方法二

1.自定义一个类,啥接口也不用实现

package com.why.aop;
import org.springframework.stereotype.Component;

/**
 * @program: AOP
 * @description:
 * @author: @why
 * @create: 2020-08-31 22:08
 **/
@Component

public class MyLog {
   public void Before(){
       System.out.println("我在你上面!!!");
   }
    public void After(){
        System.out.println("我在你下面,o((>ω< ))o");
    }

}

2.配置xml文件

<?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:context="http://www.springframework.org/schema/context"
       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/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
    <context:component-scan base-package="com.why"/>
    <aop:config proxy-target-class="true">
        <!--自定义切面,就像一把刀子一样把原本顺序执行的业务代码切开了-->
        <!--    ref是我们自定义的引用的类-->
        <aop:aspect ref="myLog" id="Knife">
<!--            找到我们的刀子要切入的点-->
            <aop:pointcut id="point" expression="execution(* com.why.serivce.ServiceIml.*(..))"/>
<!--            开切 (ˉ▽ ̄~) 切~~-->
            <aop:before method="Before" pointcut-ref="point"/>
            <aop:after method="After" pointcut-ref="point"/>
        </aop:aspect>
    </aop:config>


</beans>

3.测试代码:

 

 

posted @ 2020-08-31 22:24  why666  阅读(131)  评论(0)    收藏  举报