01.AopDemo

cool__007 于 2019-08-19 15:29:17 发布
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
1.需要被代理的对象
public interface ICalculator {
int div(int i,int j);
}
@Component(value = "calculatorImpl")
public class CalculatorImpl implements ICalculator {
@Override
public int div(int i, int j) {
System.out.println(i/j);
return i/j;
}
}
2.切面类
@EnableAspectJAutoProxy
@Component
@Aspect
public class AspectTest {
@Pointcut(value = "execution(* com.gaobin.java.aop.ICalculator.div(..))")
public void beforePointcut() {
}
@Before("beforePointcut()")
public void doBeforeCheck(){
System.out.println("Calculator Before");
}
@After("beforePointcut()")
public void doAfterCheck(){
System.out.println("Calculator After");
}
@AfterThrowing("beforePointcut()")
public void doAfterThrowingCheck(){
System.out.println("Calculator AfterThrowing");
}
@AfterReturning("beforePointcut()")
public void doAfterReturningCheck(){
System.out.println("Calculator AfterReturning");
}
}
3.启动
public class App {
public static void main(String[] args) throws InterruptedException {
ApplicationContext ac = new ClassPathXmlApplicationContext("spring-aop.xml");
ICalculator calculator = (ICalculator) ac.getBean("calculatorImpl");
calculator.div(1,0);
}
}
原文链接: 01.AopDemo https://m.mcbbbk.com/newsview396701.html
| 上一篇: 郴州宠物店宠物狗训练 | 下一篇: 1.66贴心小知识(二) |



