目录
一、pom依赖
二、买书业务接口
三、买书业务接口实现
四、junit代码测试
五、日志前置实现
六、Spring容器XML代理绑定买书业务和日志切面
七、总结
一、pom依赖
org.springframework
spring-context
5.3.20
二、买书业务接口
public interface BookService {
//买书的人 + 书名 + 单价
public boolean buy(String userName, String bookName, double price);
//买书的人 + 下单备注
public void comment(String userName, String comments);
}
三、买书业务接口实现
public class BookServiceImpl implements BookService {
@Override
public boolean buy(String userName, String bookName, double price) {
System.out.println("购买图书功能实现...........");
System.out.println(userName + "购买了" + bookName + ",花费了" + price + "元.");
System.out.println(userName + "增加了" + (price / 10));
System.out.println("图书购买业务结束");
return false;
}
@Override
public void comment(String userName, String comments) {
System.out.println("评论功能的实现 ...........");
System.out.println(userName + "发表了" + comments + ".");
System.out.println("评论功能结束 ...........");
}
}
四、junit代码测试
@Test
public void test01() {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
BookService proxy = (BookService) ac.getBean("bookService");
System.out.println(proxy.getClass());
proxy.buy("张松", "露娜月下无限连", 666);
proxy.comment("张松", "省级露娜冲国服露娜...");
}
public interface BookService {
//买书的人 + 书名 + 单价
public boolean buy(String userName, String bookName, double price);
//买书的人 + 下单备注
public void comment(String userName, String comments);
}
三、买书业务接口实现
public class BookServiceImpl implements BookService {
@Override
public boolean buy(String userName, String bookName, double price) {
System.out.println("购买图书功能实现...........");
System.out.println(userName + "购买了" + bookName + ",花费了" + price + "元.");
System.out.println(userName + "增加了" + (price / 10));
System.out.println("图书购买业务结束");
return false;
}
@Override
public void comment(String userName, String comments) {
System.out.println("评论功能的实现 ...........");
System.out.println(userName + "发表了" + comments + ".");
System.out.println("评论功能结束 ...........");
}
}
四、junit代码测试
@Test
public void test01() {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
BookService proxy = (BookService) ac.getBean("bookService");
System.out.println(proxy.getClass());
proxy.buy("张松", "露娜月下无限连", 666);
proxy.comment("张松", "省级露娜冲国服露娜...");
}
@Test
public void test01() {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
BookService proxy = (BookService) ac.getBean("bookService");
System.out.println(proxy.getClass());
proxy.buy("张松", "露娜月下无限连", 666);
proxy.comment("张松", "省级露娜冲国服露娜...");
}
思考:业务功能正常执行,为什么执行之前会先打印系统日志信息呢?
答:因为另外实现了日志AOP切面功能,使用前置通知绑定依赖在买书方法上,当执行买书业务时,一定会先执行系统日志功能。这就是Spring容器AOP切面的原生实现。
五、日志前置实现
public class LogAdvice implements MethodBeforeAdvice {
@Override
public void before(Method method, Object[] objects, Object o) throws Throwable {
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
System.out.println("n[系统日志]" + sf.format(new Date()) + "---" + method.getName() + Arrays.toString(objects));
}
}
回顾Before通知:在目标方法被调用前调用,涉及接口org.springframework.aop.MethodBeforeAdvice;
六、Spring容器XML代理绑定买书业务和日志切面
使用Spring框架内置的动态代理工厂,传入参数,绑定业务,就能实现AOP织入。底层使用JDK动态代理和CGLib,根据目标对象有没有实现接口,动态选择代理模式进行业务代理。
logAdvice
七、总结
回顾Before通知:在目标方法被调用前调用,涉及接口org.springframework.aop.MethodBeforeAdvice;
使用Spring框架内置的动态代理工厂,传入参数,绑定业务,就能实现AOP织入。底层使用JDK动态代理和CGLib,根据目标对象有没有实现接口,动态选择代理模式进行业务代理。
logAdvice
七、总结
仅自己学习记录,如有错误,敬请谅解~,谢谢~~~



