zoukankan      html  css  js  c++  java
  • 学习Spring中遇到关于BeanFactory及测试类的问题

    最近在学习Spring,使用的是Spring 5.0.1 学习书本中使用的是4.0

    学习书本中使用以下来加载配置文件及设置

    Resource resource = new ClassPathResource("applicationContext.xml"); //加载配置文件
    BeanFactory factory = new XmlBeanFactory(resource);
    Student student= (Student) factory.getBean("student");
    student.setName("李明");
    student.setAge(18);
    mystudentBean.setSex("男");
    System.out.println(student);

    结果XmlBeanFactory(resource)那里出现了已过时,后面上网查了一下使用了如下方式

    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
            BeanFactory factory = context;
            Student student= (Student) factory.getBean("student");
            student.setName("李明");
            student.setAge(18);
            student.setSex("男");
            System.out.println(student);

    在Junit test类中运行的时候出现了如下错误:

    java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
        at org.springframework.context.support.AbstractApplicationContext.<init>(AbstractApplicationContext.java:160)
        at org.springframework.context.support.AbstractApplicationContext.<init>(AbstractApplicationContext.java:230)
        at org.springframework.context.support.AbstractRefreshableApplicationContext.<init>(AbstractRefreshableApplicationContext.java:92)
        at org.springframework.context.support.AbstractRefreshableConfigApplicationContext.<init>(AbstractRefreshableConfigApplicationContext.java:59)
        at org.springframework.context.support.AbstractXmlApplicationContext.<init>(AbstractXmlApplicationContext.java:62)
        at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:141)
        at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85)
        at com.itzcn.Test.BeanFactoryTest.test(BeanFactoryTest.java:25)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
        at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
        at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
        at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
        at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
    Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        ... 31 more

    原因是因为缺少了commons-logging-1.1.3.jar包,加入该包后可以正常运行测试了

    写以上是为了方便记录自己学习过程中遇到问题的解决方式,避免出现类似的问题时不知道是什么原因。

  • 相关阅读:
    C#反射概念以及实例详解【转】
    .NET(C#):使用反射来获取枚举的名称、值和特性【转】
    探求C#.Net中ArrayList与Array的区别 【转】
    C#中IList<T>与List<T>的区别感想【转】
    C# System.Guid.NewGuid() 【转】
    回车键触发按钮事件
    MVC中Json的使用:Controller中Json的处理【转】
    关于优化性能<主要是速度方面>的个人心得 【转】
    ca72a_c++_标准IO库:面向对象的标准库
    ca71a_c++_指向函数的指针_通过指针调用函数txwtech
  • 原文地址:https://www.cnblogs.com/lichengcai/p/Spring.html
Copyright © 2011-2022 走看看