springAop基于注解配置

41 篇文章 0 订阅

测试Java类

package com.test.daselin.generate.aspectjdemo;

public interface ArithmeticCalculator {

    Integer getPlus(Integer i,Integer j);

    Integer getMinus(Integer i,Integer j);

    Integer getMultiply(Integer i,Integer j);

    Integer getExcept(Integer i,Integer j);

}
package com.test.daselin.generate.aspectjdemo;

import org.springframework.stereotype.Component;

@Component
public class ArithmeticCalculatorImpl implements ArithmeticCalculator {

    @Override
    public Integer getPlus(Integer i, Integer j) {
        return i+j;
    }

    @Override
    public Integer getMinus(Integer i, Integer j) {
        return i-j;
    }

    @Override
    public Integer getMultiply(Integer i, Integer j) {
        return i*j;
    }

    @Override
    public Integer getExcept(Integer i, Integer j) {
        if(j!=0){
            return i/j;
        }else{
            return -1;
        }
    }

}

切面

package com.test.daselin.generate.aspectjdemo;

import java.util.Arrays;
import java.util.List;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

/**
 * logging aspectj
 * @author Toshiba
 *
 */
@Aspect
@Component
public class LoggingAspectj {

    @Before(value = "execution(public Integer com.test.daselin.generate.aspectjdemo.ArithmeticCalculator.*(Integer,Integer))")
    public void beforeMethod(JoinPoint joinPoint){
        String name = joinPoint.getSignature().getName();
        List<Object> args = Arrays.asList(joinPoint.getArgs());
        System.out.println("the method "+name+" begin with"+args);
    }
}

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:c="http://www.springframework.org/schema/c"
    xmlns:cache="http://www.springframework.org/schema/cache"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:lang="http://www.springframework.org/schema/lang"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:task="http://www.springframework.org/schema/task"
    xmlns:util="http://www.springframework.org/schema/util"
    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-4.0.xsd
        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.0.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

    <context:component-scan base-package="com.test.daselin.generate.aspectjdemo"></context:component-scan>
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy> 
</beans>

关于aop注解
这里写图片描述
关于aop的execution
这里写图片描述
这里写图片描述
第一个*表示通用的标识和返回值。
第二个*表示通用的类
第三个*表示通用的方法。
还可以一起拼接
这里写图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值