SpringAOP基于schema-based和Aspectj

schema-baed方式       (书写多个类,分别实现不同接口并重写方法)

前置类实现       MethodBeforeAdvice  并重写方法

后置类实现        AfterReturningAdvice   并重写方法

环绕实现        MethodInterceptor    参数MethodInvocation作为切点方法  在调用此方法运行前后可以设置环绕前置和环绕后置

异常实现        ThrowsAdvice        并重写方法,报异常就近原则,显示异常信息也是就近原则

public class UserServiceImpl implements UserService {

    @Override
    public void sayHello() {
        System.out.println("sayHello无参方法");
    }

    @Override
    public String showInfo() {
        System.out.println("打印结果showInfo方法");
        return "showInfo返回结果";
    }

    @Override
    public String sayHello(String name) {
        System.out.println("sayHello有参方法"+name);
        return "返回结果sayHello有参方法";
    }

    @Override
    public User login(String username, String password) throws Exception {
        System.out.println("service自己的本身username"+username+"password的值"+password);
        if(username==null){
            throw new RuntimeException();
        }
        return new User();
    }
}
<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd"
       default-autowire="default">


        <bean name="userService" class="com.demo.service.impl.UserServiceImpl"/>
    
        <bean id="beforeMethod" class="com.demo.advice.MyBeforeAdvice"/>
        <bean id="afterMethod" class="com.demo.advice.MyAfterMethod"/>
        <bean id="aroundMethod" class="com.demo.advice.MyAroundMethod"/>

<aop:config>
        <aop:pointcut id="firstPointCut" expression="execution(void com.demo.service.impl.UserServiceImpl.sayHello())"/>
        <aop:pointcut id="secondPointCut" expression="execution(java.lang.String com.demo.service.impl.UserServiceImpl.showInfo())"/>
        <aop:pointcut id="thirdPointCut" expression="execution(void com.demo.service.impl.UserServiceImpl.sayHello(java.lang.String))"/>
        <aop:pointcut id="allPointCut" expression="execution(* com.demo.service.*.*(..))"/>
    
        <aop:advisor advice-ref="afterMethod" pointcut-ref="firstPointCut"/>
        <aop:advisor advice-ref="beforeMethod" pointcut-ref="firstPointCut"/>
        <aop:advisor advice-ref="aroundMethod" pointcut-ref="firstPointCut"/>

</aop:config>
</beans>

AspectJ方式(一个类书写多个方法,不用实现其他接口)

不传参方式


public class FirstAspectAdvice {
    public void before() throws Throwable{
        System.out.println("aspect前置");
    }

    public void afterRunning(Object returnValue) throws Throwable{
        System.out.println("后置通知,返回值是"+returnValue);
    }

    public Object around(ProceedingJoinPoint point) throws Throwable{
        Object returnValue = null;
        try{
            System.out.println("环绕前置");
            Object proceed = point.proceed();
            System.out.println("环绕后置");
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("环绕异常");
            throw e;
        }
        return returnValue;
    }

    public void throwing(Exception ex) throws Throwable{
        System.out.println("aspect异常");
    }
}

  <aop:aspect ref="firstAspect">
                <aop:pointcut id="all" expression="execution(* com.demo.service.*.*(..))"/>
                <aop:before method="before" pointcut-ref="all"/>
                <aop:after-returning method="afterRunning" pointcut-ref="all"
                                     returning="returnValue"/>
                <aop:around method="around" pointcut-ref="all"/>
                <aop:after-throwing method="throwing" pointcut-ref="all" throwing="ex"/>
 </aop:aspect>

传参方式

public class SecondAspectAdvice {
    public void before(String username,String password){
        System.out.println("second前置 username"+username);
    }
    public void after(Object returnValue,String username,String password){
        System.out.println("后置 password"+password);

    }
    public void around(ProceedingJoinPoint point,String username,String password) throws Throwable {
        System.out.println("环绕前置 username"+username);
        Object proceed = point.proceed();
        System.out.println("环绕后置 password"+password);

    }

    public void throwing(Exception ex,String username,String password){
        System.out.println("异常");
    }

}
<bean id="secondAspect" class="com.demo.aspect.SecondAspectAdvice"/>

    <aop:config>
        <aop:aspect ref="secondAspect">
            <aop:pointcut id="all"
                          expression="execution(com.demo.pojo.User com.demo.service.UserService.login(java.lang.String,java.lang.String)) and args(username,password)"/>
            <aop:before method="before" pointcut-ref="all" arg-names="username,password"/>
            <aop:after-returning method="after" pointcut-ref="all" arg-names="returnValue,username,password" returning="returnValue"/>
            <aop:around method="around" pointcut-ref="all" arg-names="point,username,password"/>
            <aop:after-throwing method="throwing" pointcut-ref="all" arg-names="ex,username,password" throwing="ex" />
        </aop:aspect>
    </aop:config>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值