Exception notification class
public class MyThrowAdvice {
public void MyThrow(Exception e) {
System.out.println("throw Exception"+e.getMessage());
}
}
Demo type
Write a 5/0 in the Demo1 method, let it report an exception
public class Demo {
public void Demo1(){
int i=5/0;
System.out.println("demo1");
}
public void Demo2(){
System.out.println("demo2");
}
public void Demo3(){
System.out.println("demo3");
}
}
applicationContext.xml part of the code
- Exceptions can only be achieved by aspectJ
- Need to write in the aspect tag
- Like schema-based, there are pointcut tags, pointcut tags, notification tags, and beans with notification tags introduced by ref
<bean id="mythrow" class="cn.wit.advice.MyThrowAdvice"> </bean>
<aop:config>
<aop:aspect ref="mythrow">
<aop:pointcut expression="execution(* cn.wit.test.Demo.Demo1())" id="mypoint"/>
<aop:after-throwing method="MyThrow" pointcut-ref="mypoint" throwing="e"/>
</aop:aspect>
</aop:config>
<bean id="demo" class="cn.wit.test.Demo"></bean>
test
public class Test {
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
Demo demo = ac.getBean("demo",Demo.class);
try {
demo.Demo1();
} catch (Exception e) {
}
}
}