applicationContext.xml
- Point-cut label, point-cut object
- Notification label, notification object
- The aspectJ method is to define the method yourself, the notification tag only specifies the method name in the notification class, and then write the class name of the notification class on the aspect tag
- Schema-based is a fixed method, the method name in the exception notification class must be afterThrowing, so the notification class can be specified in the notification tag
<?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"
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.xsd">
<bean id="mythrow" class="cn.wit.advice.MyThrow"></bean>
<aop:config>
<aop:pointcut expression="execution(* cn.wit.test.Demo.Demo1())" id="mypoint"/>
<aop:advisor advice-ref="mythrow" pointcut-ref="mypoint"/>
</aop:config>
<bean id="demo" class="cn.wit.test.Demo"></bean>
</beans>
Exception notification class
The method name must be afterThrowing
public class MyThrow implements ThrowsAdvice{
public void afterThrowing(Exception ex) throws Throwable{
System.out.println("异常通知");
}
}
Demo type
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");
}
}
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) {
}
}
}