Spring的AOP代理時出現的ClassCastException: $Proxy0 cannot be cast to (com......) 的解決辦法
這是我的測試文件代碼,EmployeeService類實現了EmployeeServiceInter的接口,一下這段代碼是創建的EmployeeService對象,結果報錯。
public class MyTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
EmployeeService employeeService=(EmployeeService) ac.getBean("employeeService");
Employee e=new Employee("lidawei", "123", "[email protected]", new java.util.Date(), 8000f);
employeeService.addEmployee(e);
}
}
只要將修改後EmployeeServiceInter接口實現動態代理類即可,完美運行。修改後的代碼如下:public class MyTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
EmployeeServiceInter employeeService=(EmployeeServiceInter) ac.getBean("employeeService");
Employee e=new Employee("lidawei", "123", "[email protected]", new java.util.Date(), 8000f);
employeeService.addEmployee(e);
}
}