20221019 Spring AOP

Spring AOP

xml方式

切面类

public class MyAop {
    public void Before(){
        System.out.println("前置通知");
    }
    public void After(){
        System.out.println("后置通知");
    }
}

目标类

@Component
public class Service {
    public void say(){
        System.out.println("Service 执行");
    }


}

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: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/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">

    <!--加入注解的支持-->
    <context:annotation-config></context:annotation-config>
    <!--扫描指定的路径-->
    <context:component-scan base-package="com.zzm"></context:component-scan>

    <bean class="com.zzm.aop.MyAop" id="myAop"></bean>


    <!--设置spring AOP支持类的代理-->
    <aop:aspectj-autoproxy proxy-target-class="true"/>
    <!--    Aop配置-->
    <aop:config>
        <!--切面的配置aspect -->
        <aop:aspect ref="myAop">
            <!--切入点,我们的切面类的通知要放在哪些位置, expression 表达式告知切入点集合位置-->
            <aop:pointcut id="point" expression="execution(* com.zzm.aop.Service.say())"/>
            <!--前置通知,目标方法执行之前执行的方法-->
            <aop:before method="Before" pointcut-ref="point"></aop:before>
            <!--后置通知,目标方法执行之后执行的方法-->
            <aop:after method="After" pointcut-ref="point"></aop:after>
        </aop:aspect>
    </aop:config>


    

</beans>

测试

public class AopTest {
    @Test
    public void go(){
        ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        //使用的是类
        Service service = (Service) classPathXmlApplicationContext.getBean("service");
        service.say();

    }
}

使用注解方式

切面类

@Aspect
@Component
public class AOP {
    @Before(value = "execution(* com.etc.annotation.Service.go())")
    public void before(){
        System.out.println("前置通知....");
    }

    @After(value = "execution(* com.etc.annotation.Service.go())")
    public void after(){
        System.out.println("后置通知....");
    }

    @Around(value = "execution(* com.etc.annotation.Service.go())")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        System.out.println("环绕前置通知....");
        Object proceed = point.proceed();
        System.out.println("环绕后置通知....");
        return proceed;
    }

}

目标接口和目标类

public interface Service {
    public void go();
}
@Component
public class ServiceImpl implements Service{
    @Override
    public void go() {
        System.out.println("服务运行中.....");
    }
}

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: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/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd ">

    <context:annotation-config></context:annotation-config>

    <context:component-scan base-package="com.etc"></context:component-scan>
    <!-- 需要告知spring 此时我们使用注解配置了和aop有关的@Aspect @Before @After等等 -->
    <aop:aspectj-autoproxy proxy-target-class="true"/>




</beans>

测试

public class AOPTest {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        //使用接口
//        Service service = (Service) applicationContext.getBean("serviceImpl");
        //使用类 确保 proxy-target-class="true"
        ServiceImpl serviceImpl = (ServiceImpl) applicationContext.getBean("serviceImpl");
        serviceImpl.go();
    }
}

切面类切入点冗余解决

@Aspect()
@Component
public class AOP {

    @Pointcut(value = "execution(* com.etc.annotation.Service.go())")
    public void method(){}

    @Before(value = "method()")
    public void before(){
        System.out.println("前置通知....");
    }

    @After(value = "method()")
    public void after(){
        System.out.println("后置通知....");
    }

    @Around(value = "method()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        System.out.println("环绕前置通知....");
        Object proceed = point.proceed();
        System.out.println("环绕后置通知....");
        return proceed;
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值