报错:
java.lang.ClassCastException: com.sun.proxy.$Proxy91 cannot be cast to com.zhl.services.book.AnshaoServic
解决办法:
不能用接口的实现类(UserServiceImpl01_AOP)来转换Proxy的实现类,它们是同级,应该用共同的接口来转换
dao:
public interface AnshaoDao
service:
@Service
public class AnshaoService implements AnshaoDao
解决方案:
public class AnshaoServiceTest {
private ApplicationContext applicationContext;
@Before
public void setUp() throws Exception {
applicationContext = new FileSystemXmlApplicationContext("classpath:spring/applicationContext.xml");
}
@Test
public void test() {
//错误代码:
AnshaoService anshaoService = (AnshaoService) applicationContext.getBean("anshaoService");
//正确代码
AnshaoDao anshaoDao = (AnshaoDao) applicationContext.getBean("anshaoService");
anshaoDao.addAll();
}
}