关于eclipse:无法实例化[org.springframework.web.context.support.XmlWebApplicationContext]

Failed to instantiate [org.springframework.web.context.support.XmlWebApplicationContext]

本问题已经有最佳答案,请猛点这里访问。

我是J2EE新手(.Net堆栈),并且竭尽全力尝试着手进行基础教程。我正在使用具有Tomcat 8.5设置的Eclipse。

尝试启动我的应用程序时,我在控制台中始终看到此错误...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
INFO: Initializing Spring root WebApplicationContext
09:39:42,442 ERROR ContextLoader:351 - Context initialization failed
org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.context.support.XmlWebApplicationContext]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:159)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:104)
    at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:380)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:313)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4727)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5189)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1403)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1393)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
    at org.springframework.core.io.support.PathMatchingResourcePatternResolver.<clinit>(PathMatchingResourcePatternResolver.java:169)
    at org.springframework.web.context.support.AbstractRefreshableWebApplicationContext.getResourcePatternResolver(AbstractRefreshableWebApplicationContext.java:183)
    at org.springframework.context.support.AbstractApplicationContext.<init>(AbstractApplicationContext.java:216)
    at org.springframework.context.support.AbstractRefreshableApplicationContext.<init>(AbstractRefreshableApplicationContext.java:80)
    at org.springframework.context.support.AbstractRefreshableConfigApplicationContext.<init>(AbstractRefreshableConfigApplicationContext.java:50)
    at org.springframework.web.context.support.AbstractRefreshableWebApplicationContext.<init>(AbstractRefreshableWebApplicationContext.java:95)
    at org.springframework.web.context.support.XmlWebApplicationContext.<init>(XmlWebApplicationContext.java:62)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147)
    ... 13 more
Sep 09, 2016 9:39:42 AM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.context.support.XmlWebApplicationContext]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:159)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:104)
    at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:380)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:313)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4727)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5189)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1403)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1393)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
    at org.springframework.core.io.support.PathMatchingResourcePatternResolver.<clinit>(PathMatchingResourcePatternResolver.java:169)
    at org.springframework.web.context.support.AbstractRefreshableWebApplicationContext.getResourcePatternResolver(AbstractRefreshableWebApplicationContext.java:183)
    at org.springframework.context.support.AbstractApplicationContext.<init>(AbstractApplicationContext.java:216)
    at org.springframework.context.support.AbstractRefreshableApplicationContext.<init>(AbstractRefreshableApplicationContext.java:80)
    at org.springframework.context.support.AbstractRefreshableConfigApplicationContext.<init>(AbstractRefreshableConfigApplicationContext.java:50)
    at org.springframework.web.context.support.AbstractRefreshableWebApplicationContext.<init>(AbstractRefreshableWebApplicationContext.java:95)
    at org.springframework.web.context.support.XmlWebApplicationContext.<init>(XmlWebApplicationContext.java:62)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147)
    ... 13 more

我的配置文件如下所示...

applicationContext.xml ...

1
2
3
4
5
6
7
8
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <import resource="datasource-config.xml" />
    <import resource="webflow-config.xml" />        
</beans>

datasource-config.xml ...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd    
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

    <context:property-placeholder location="classpath:datasource.properties" />

    <bean id="dataSource" class="oracle.jdbc.pool.OracleDataSource" destroy-method="close">
        <property name="connectionCachingEnabled" value="true" />
        <property name="URL" value="jdbc:oracle:thin:@localhost:1521:XE" />
        <property name="user" value="user" />
        <property name="password" value="pass" />
    </bean>        

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="showSql" value="true" />
                <property name="generateDdl" value="true" />
                <property name="databasePlatform" value="org.hibernate.dialect.Oracle10gDialect" />
            </bean>
        </property>
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="dataSource" ref="dataSource" />
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" />

    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

</beans>

webflow-config.xml ...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:webflow="http://www.springframework.org/schema/webflow-config"
       xmlns:faces="http://www.springframework.org/schema/faces"
       xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/webflow-config
            http://www.springframework.org/schema/webflow-config/spring-webflow-config.xsd
            http://www.springframework.org/schema/faces
            http://www.springframework.org/schema/faces/spring-faces.xsd">

    <bean id="jpaFlowExecutionListener" class="org.springframework.webflow.persistence.JpaFlowExecutionListener">
        <constructor-arg ref="entityManagerFactory" />
        <constructor-arg ref="transactionManager" />
    </bean>

    <bean id="facesContextListener" class="org.springframework.faces.webflow.FlowFacesContextLifecycleListener" />

    <webflow:flow-executor id="flowExecutor">
        <webflow:flow-execution-listeners>
            <webflow:listener ref="jpaFlowExecutionListener" />
            <webflow:listener ref="facesContextListener"/>
        </webflow:flow-execution-listeners>
    </webflow:flow-executor>            

    <webflow:flow-registry id="flowRegistry" flow-builder-services="facesFlowBuilderServices" base-path="/WEB-INF/flows">
        <webflow:flow-location-pattern value="/**/*-flow.xml" />
    </webflow:flow-registry>

    <faces:flow-builder-services id="facesFlowBuilderServices" development="true" />
    <faces:resources />

    <bean class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping">
        <property name="order" value="1"/>
        <property name="flowRegistry" ref="flowRegistry" />
        <property name="defaultHandler">
            <bean class="org.springframework.web.servlet.mvc.UrlFilenameViewController" />
        </property>
    </bean>

    <bean class="org.springframework.faces.webflow.JsfFlowHandlerAdapter">
        <property name="flowExecutor" ref="flowExecutor" />
    </bean>

    <bean id="faceletsViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass" value="org.springframework.faces.mvc.JsfView"/>
        <property name="prefix" value="/WEB-INF/" />
        <property name="suffix" value=".xhtml" />
    </bean>

    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />

</beans>

像这样的东西让我想起了为什么我这么鄙视声明式编程。

我的pom XML看起来像这样...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example.j2eeapp</groupId>
  j2eeapplication</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <name>J2EE Application</name>

  <repositories>
    <repository>
        <id>prime-repo</id>
        <name>PrimeFaces Maven Repository</name>
        <url>http://repository.primefaces.org</url>
    </repository>
  </repositories>

  <dependencies>
      <dependency>
        <groupId>com.oracle</groupId>
        ojdbc14</artifactId>
        <version>10.2.0.1.0</version>
      </dependency>
      <dependency>
        <groupId>org.hibernate</groupId>
        hibernate-core</artifactId>
        <version>5.2.2.Final</version>
      </dependency>
      <dependency>
        <groupId>org.hibernate</groupId>
        hibernate-validator</artifactId>
        <version>5.2.2.Final</version>
      </dependency>
      <dependency>
        <groupId>junit</groupId>
        junit</artifactId>
        <version>4.8.2</version>
        <scope>test</scope>
      </dependency>
      <dependency>
        <groupId>org.springframework.webflow</groupId>
        spring-webflow</artifactId>
        <version>2.4.4.RELEASE</version>
      </dependency>
      <dependency>
        <groupId>org.springframework.webflow</groupId>
        spring-faces</artifactId>
        <version>2.4.4.RELEASE</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        spring-orm</artifactId>
        <version>4.3.2.RELEASE</version>
      </dependency>
      <dependency>
        <groupId>com.sun.faces</groupId>
        jsf-impl</artifactId>
        <version>2.2.13</version>
      </dependency>
      <dependency>
        <groupId>com.sun.facelets</groupId>
        jsf-facelets</artifactId>
        <version>1.1.14</version>
      </dependency>
      <dependency>
        <groupId>com.sun.faces</groupId>
        jsf-api</artifactId>
        <version>2.2.13</version>
      </dependency>
      <dependency>
        <groupId>commons-dbcp</groupId>
        commons-dbcp</artifactId>
        <version>20030825.184428</version>
      </dependency>
      <dependency>
        <groupId>org.slf4j</groupId>
        slf4j-log4j12</artifactId>
        <version>1.7.21</version>
      </dependency>
      <dependency>
        <groupId>javax.servlet</groupId>
        javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        spring-tx</artifactId>
        <version>4.3.2.RELEASE</version>
      </dependency>
      <dependency>
        <groupId>xml-apis</groupId>
        xml-apis</artifactId>
        <version>2.0.2</version>
      </dependency>
      <dependency>
        <groupId>org.springframework.security</groupId>
        spring-security-web</artifactId>
        <version>4.1.3.RELEASE</version>
      </dependency>
      <dependency>
        <groupId>org.springframework.security</groupId>
        spring-security-config</artifactId>
        <version>4.1.3.RELEASE</version>
      </dependency>
      <dependency>
        <groupId>org.primefaces</groupId>
        primefaces</artifactId>
        <version>6.0</version>
      </dependency>
      <dependency>
        <groupId>cglib</groupId>
        cglib</artifactId>
        <version>3.2.4</version>
      </dependency>
      <dependency>
        <groupId>org.hibernate</groupId>
        hibernate-entitymanager</artifactId>
        <version>5.2.2.Final</version>
      </dependency>
      <dependency>
        <groupId>log4j</groupId>
        log4j</artifactId>
        <version>1.2.16</version>
      </dependency>
  </dependencies>

</project>

您在项目构建路径中缺少记录器依赖项。
如果您使用的是通用记录器,则在pom.xml

中添加以下依赖项

1
2
3
4
5
    <dependency>
        <groupId>commons-logging</groupId>
        commons-logging</artifactId>
        <version>1.2</version>
    </dependency>

如果使用的是logback,则slf4j然后排除常见的日志记录并使用以下依赖项:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
    <exclusions>
            <exclusion>
                commons-logging</artifactId>
                <groupId>commons-logging</groupId>
            </exclusion>
    </exclusions>

 </dependency>
    <groupId>org.slf4j</groupId>
    slf4j-api</artifactId>
    <version>${org.slf4j-version}</version>    
</dependency>

    <dependency>
        <groupId>ch.qos.logback</groupId>
        logback-classic</artifactId>
        <version>1.0.13</version>
    </dependency>

    <dependency>
        <groupId>ch.qos.logback</groupId>
        logback-core</artifactId>
        <version>1.0.13</version>
    </dependency>

    <dependency>
        <groupId>ch.qos.logback</groupId>
        logback-access</artifactId>
        <version>1.0.13</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        jcl-over-slf4j</artifactId>
        <version>1.7.5</version>
    </dependency>


原因:java.lang.NoClassDefFoundError:org / apache / commons / logging / LogFactory

错误与缺少依赖项有关,您的类路径中缺少commons-logging jar。您的POM.xml可能缺少

1
2
3
4
5
6
<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
<dependency>
    <groupId>commons-logging</groupId>
    commons-logging</artifactId>
    <version>1.2</version>
</dependency>

,并且您可能想排除依赖于'commons-logging'的spring webflow传递式

1
2
3
4
5
6
7
8
9
10
11
<dependency>
        <groupId>org.springframework.webflow</groupId>
        spring-webflow</artifactId>
        <version>2.4.4.RELEASE</version>
        <exclusions>
            <exclusion>
              <groupId>commons-logging</groupId>
              commons-logging</artifactId>
            </exclusion>
        </exclusions>
      </dependency>