初学Sping
main方法又不会给你加载spring的配置文件,想在main方法运行,就手动去加载spring的配置文件
一楼正解,main方法加载不到spring管理的bean,所以会报错空指针。你想测试,应该写一个获取bean工具类,在项目启动的时候,在测试类里调用
你需要通过spring上下文先获取到bean,不然会空指针。。ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationcontext.xml");
DataSource ds = (DataSource) ctx.getBean("dataSource");需要注意文件路径。。。