五种增强注解
AspectJ共支持五种类型的增强注解:
- 前置增强 @Before
- 后置增强@Aspect
- 返回增强@AfterReturning
- 异常增强@AfterThrowing
- 环绕增强@Around
示例中涉及到的源码:
1.ICalculatorService
@Service
public interface ICalculatorService {
int add(int a,int b);
int sub(int a,int b);
int mul(int a,int b);
int div(int a,int b);
}
2.CalculatorService
@Service
public class CalculatorService implements ICalculatorService {
@Override
public int add(int a, int b) {
int result = a+b;
return result;
}
@Override
public int sub(int a, int b) {
int result = a-b;
return result;
}
@Override
public int mul(int a, int b) {
int result = a*b;
System.out.println(result);
return result;
}
@Override
public int div(int a, int b) {
int result = a/b;
return result;
}
}
3.Spring XML配置(application.xml):
<context:component-scan base-package="com.jd"></context:component-scan>
<aop:aspectj-autoproxy proxy-target-class="false"></aop:aspectj-autoproxy>
4.Test测试类
public class Test {
public static void main(String[] args) {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");//创建IOC容器
ICalculatorService calculatorService = applicationContext.getBean(ICalculatorService.class);