一、导入jar包
新建一个lib文件夹把jar包导入进去
二、在src里创建com.li.advice、service、servlet包
三、在包里书写类和接口
package com.li.service.impl;
import com.li.service.BookService;
/**
* 核心类
*/
public class BookServiceImpl implements BookService {
@Override
public int save(int n){
System.out.println("添加");
return 1;
}
@Override
public int del(){
System.out.println("删除");
return 1;
}
@Override
public int update(){
System.out.println("修改");
return 1;
}
@Override
public void find(){
System.out.println("查询");
}
}
package com.li.service;
public interface BookService {
int save(int n);
int del();
int update();
void find();
}
package com.li.advice;
/**
* 增强类
*/
public class Loger {
public void check(){
System.out.println("前置通知/增强:执行系统的权限验证");
}
public void logPrint(){
System.out.println("后置通知/增强:执行日志的打印");
}
public void exception(){
System.out.println("异常通知/增强:执行日志的打印");
}
public void distory(){
System.out.println("最终通知/增强:执行日志的打印");
}
}
四、书写测试类
package com.li.servlet;
import com.li.service.BookService;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test01 {
@Test
public void test01(){
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
BookService bookService =context.getBean(BookService.class);
bookService.save(8);
bookService.del();
bookService.update();
bookService.find();
// try{
// System.out.println("1.功能之前");
//
// System.out.println("2.功能执行");
// int a=5/0;
//
// System.out.println("3.功能之后在返回之前");
//
// return;
// }catch (Exception e){
// System.out.println("4.异常执行");
// }finally {
// System.out.println("5.最终执行");
// }
}
}
运行结果: