【模拟spring框架流程,bean注入类型过程中执行增加数据的操作报空指针的异常 _数据库 】 | IT修真院·坑乎
咨询电话 : 010-59478634
切换导航
首页
我的提问
我的回答
我的点赞
消息通知
个人主页
×
提示
尚未登陆,前往官网登陆?
×
提示
尚未登陆,前往官网登陆?
模拟spring框架流程,bean注入类型过程中执行增加数据的操作报空指针的异常
我也踩过这个坑(
1
)
已统计您的踩坑,无需重复点击
回答(0)
数据库
详细描述
模拟spring框架流程,在ServiceTest类中执行增加数据的操作报空指针的异常,具体的详细错误截图和url可见线面的错误截图和url路径。
错误截图
源码
测试类UserServiceTest: public class UserServiceTest { @Test public void testAdd()throws Exception{ BeanFactory applicationContext = new ClassPathXmlApplicationContext(); UserService userService = (UserService)applicationContext.getBean("userService"); User user = new User(); user.setName("小三"); user.setAge(18); user.setSex("女"); UserDaoImpl userDaoImpl = new UserDaoImpl(); userDaoImpl.save(user); System.out.println(user); userService.add(user); } } 类ClassPathXmlApplicationContext: public class ClassPathXmlApplicationContext implements BeanFactory{ //启动程序的时候需要把配置文件的数据先读到容器中 //需要使用的时候再取出来 private Map
beans = new HashMap
(); public ClassPathXmlApplicationContext()throws Exception{ ///使用JDOM首先要指定使用什么解析器 这表示使用的是默认的解析器 SAXBuilder saxBuilder = new SAXBuilder(); /* ClassPathXmlApplicationContext.class.getClassLoader().getResourceAsStream(file); 1.调用对象的getClass()方法是获得对象当前的类类型,这部分数据存在方法区中, 2.在类类型上调用getClassLoader()方法是得到当前类型的类加载器,在Java中所有的类都是通过加载器加载到虚拟机中的, 而且类加载器之间存在父子关系,就是子知道父,父不知道子,这样不同的子加载的类型之间是无法访问的(虽然它们都被放在方法区中), 所以在这里通过当前类的加载器来加载资源也就是保证是和类类型同一个加载器加载的。 最后调用了类加载器的getResourceAsStream()方法来加载文件资源 */ Document document = saxBuilder.build(this.getClass().getClassLoader().getResourceAsStream("beans.xml")); Element root = document.getRootElement(); List list = root.getChildren("bean"); for (int i = 0;i
)element.getChildren("property")){ String name = propertyElement.getAttributeValue("name");//UserDao String ref = propertyElement.getAttributeValue("ref");//UserDaoImpl Object beanOject = beans.get(ref); String methodName = "set" + name.substring(0,1).toUpperCase() + name.substring(1); System.out.println("method name = " + methodName); //反射 Method method = o.getClass().getMethod(methodName,beanOject.getClass().getInterfaces()[0]); method.invoke(o,beanOject); } } System.out.println("beans.xml中类已解析完毕,已存入map中等待调用"); } public Object getBean(String id) { return beans.get(id); } } beans.xml:
url
点我跳转源码链接
编辑于2025-04-15
Copyright ©2015 北京葡萄藤信息技术有限公司 All Rights Reserved | 京ICP备15035574号-1
复制链接
新浪微博
微信扫一扫
1345
0
10
模拟spring框架流程,bean注入类型过程中执行增加数据的操作报空指针的异常
0
1