测试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
第一个*表示通用的标识和返回值。
第二个*表示通用的类
第三个*表示通用的方法。
还可以一起拼接