package com.fruitking.proxy.springaop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.fruitking.proxy.CarService;
public class TestSpringAOP {
/**
* 利用spring的AOP机制实现“代理”的横向抽取机制方式
* @param args
*/
public static void main(String[] args) {
ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
CarService carService = (CarService) ctx.getBean("carService");
carService.start();
carService.getLoadAmount();
String driver = carService.setDriver("fruitking");
System.out.println(driver);
System.out.println("------------------------------");
carService.loadGoods("Miss Mary");
System.out.println("------------------------------");
try{
carService.loadGoods(null);
}catch(NullPointerException e){
e.printStackTrace();
}
System.out.println("------------------------------");
try{
carService.loadGoods("tiger");
}catch(IllegalArgumentException e){
e.printStackTrace();
}
}
}
代理模式,JDK动态代理,SpringAOP来龙去脉(2)
本文通过一个具体的示例展示了如何使用 Spring 的 AOP 功能来实现面向切面编程。通过配置文件 beans.xml 注入 CarService 接口的实现,并演示了如何在不修改原有业务逻辑的基础上增加新的行为。

被折叠的 条评论
为什么被折叠?



