当前位置: 动力学知识库 > 问答 > 编程问答 >

Exception while executing maven Spring web mvc in web.xml

问题描述:

Getting below exception after running code while using spring mvc and maven dependency management. Can anybody please help ? Checked answers on links below

java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext

Spring security configuration: IllegalStateException: BeanFactory not initialized or already closed

BeanFactory not initialized or already closed - call 'refresh' before

BeanFactory not initialized or already closed - call 'refresh' before accessing beans

but nothing worked.

Mar 01, 2016 4:58:59 PM org.apache.catalina.core.StandardContext listenerStart

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener

java.lang.ExceptionInInitializerError

at org.springframework.context.support.AbstractRefreshableApplicationContext.createBeanFactory(AbstractRefreshableApplicationContext.java:195)

at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:128)

at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:527)

at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:441)

at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:446)

at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:328)

at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)

at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4939)

at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434)

at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)

at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)

at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)

at java.util.concurrent.FutureTask.run(FutureTask.java:262)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)

at java.lang.Thread.run(Thread.java:744)

Caused by: java.lang.NullPointerException

at org.springframework.beans.factory.support.DefaultListableBeanFactory.<clinit>(DefaultListableBeanFactory.java:109)

... 16 more

Mar 01, 2016 4:58:59 PM org.apache.catalina.core.StandardContext startInternal

SEVERE: Error listenerStart

Mar 01, 2016 4:58:59 PM org.apache.catalina.core.StandardContext startInternal

SEVERE: Context [/m] startup failed due to previous errors

Mar 01, 2016 4:58:59 PM org.apache.catalina.core.ApplicationContext log

INFO: Closing Spring root WebApplicationContext

Mar 01, 2016 4:58:59 PM org.apache.catalina.core.StandardContext listenerStop

SEVERE: Exception sending context destroyed event to listener instance of class org.springframework.web.context.ContextLoaderListener

java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext

at org.springframework.context.support.AbstractRefreshableApplicationContext.getBeanFactory(AbstractRefreshableApplicationContext.java:172)

at org.springframework.context.support.AbstractApplicationContext.destroyBeans(AbstractApplicationContext.java:1071)

at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:1045)

at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:993)

at org.springframework.web.context.ContextLoader.closeWebApplicationContext(ContextLoader.java:583)

at org.springframework.web.context.ContextLoaderListener.contextDestroyed(ContextLoaderListener.java:116)

at org.apache.catalina.core.StandardContext.listenerStop(StandardContext.java:4980)

at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5626)

at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:232)

at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:160)

at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)

at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)

at java.util.concurrent.FutureTask.run(FutureTask.java:262)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)

at java.lang.Thread.run(Thread.java:744)

Mar 01, 2016 4:58:59 PM org.apache.coyote.AbstractProtocol start

INFO: Starting ProtocolHandler ["http-bio-8080"]

web.xml file -

<web-app xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

version="2.5">

<display-name>MongoSample</display-name>

<welcome-file-list>

<welcome-file>/WEB-INF/jsp/index.jsp</welcome-file>

</welcome-file-list>

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

<servlet>

<servlet-name>dispatcher</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>dispatcher</servlet-name>

<url-pattern>/</url-pattern>

</servlet-mapping>

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>

</context-param>

</web-app>

dispatcher-servlet.xml

<?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:context="http://www.springframework.org/schema/context"

xmlns:p="http://www.springframework.org/schema/p"

xmlns:mvc="http://www.springframework.org/schema/mvc"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd

http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">

<mvc:annotation-driven/>

<context:annotation-config/>

<context:component-scan base-package="com"/>

<mvc:resources location="/js/" mapping="/js/**" cache-period="1314000"/>

<mvc:resources location="/css/" mapping="/css/**" cache-period="1314000"/>

<bean id="mongo" class="org.springframework.data.mongodb.core.MongoFactoryBean">

<property name="host" value="localhost" />

<property name="port" value="27017"/>

</bean>

<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">

<constructor-arg name="mongo" ref="mongo" />

<constructor-arg name="databaseName" value="test" />

</bean>

<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="prefix" value="/WEB-INF/jsp/" />

<property name="suffix" value=".jsp" />

</bean>

</beans>

pom.xml

<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/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>HiberM</groupId>

<artifactId>m</artifactId>

<packaging>war</packaging>

<version>0.0.1-SNAPSHOT</version>

<name>m Maven Webapp</name>

<url>http://maven.apache.org</url>

<dependencies>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>3.8.1</version>

<scope>test</scope>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context</artifactId>

<version>4.2.4.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context-support</artifactId>

<version>4.0.0.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-core</artifactId>

<version>4.2.4.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-expression</artifactId>

<version>4.2.4.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-orm</artifactId>

<version>4.2.4.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-web</artifactId>

<version>4.2.4.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-webmvc</artifactId>

<version>4.2.4.RELEASE</version>

</dependency>

<dependency>

<groupId>commons-collections</groupId>

<artifactId>commons-collections</artifactId>

<version>3.2.2</version>

</dependency>

<dependency>

<groupId>commons-io</groupId>

<artifactId>commons-io</artifactId>

<version>2.3</version>

</dependency>

<dependency>

<groupId>commons-lang</groupId>

<artifactId>commons-lang</artifactId>

<version>2.4</version>

</dependency>

<dependency>

<groupId>commons-logging</groupId>

<artifactId>commons-logging</artifactId>

<version>1.1.1</version>

</dependency>

<dependency>

<groupId>commons-logging</groupId>

<artifactId>commons-logging-api</artifactId>

<version>1.1</version>

</dependency>

<dependency>

<groupId>log4j</groupId>

<artifactId>log4j</artifactId>

<version>1.2.12</version>

</dependency>

<dependency>

<groupId>org.springframework.data</groupId>

<artifactId>spring-data-commons</artifactId>

<version>1.11.2.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework.data</groupId>

<artifactId>spring-data-commons-core</artifactId>

<version>1.2.0.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework.data</groupId>

<artifactId>spring-data-mongodb</artifactId>

<version>1.8.2.RELEASE</version>

</dependency>

<dependency>

<groupId>javax.persistence</groupId>

<artifactId>persistence-api</artifactId>

<version>1.0.2</version>

</dependency>

<dependency>

<groupId>javassist</groupId>

<artifactId>javassist</artifactId>

<version>3.12.0.GA</version>

</dependency>

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>jstl</artifactId>

<version>1.2</version>

</dependency>

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>servlet-api</artifactId>

<version>2.5</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-beans</artifactId>

<version>4.2.4.RELEASE</version>

</dependency>

</dependencies>

<build>

<finalName>m</finalName>

</build>

</project>

Have executed/run the application without using maven by adding jars to lib folder it worked fine, but when using maven it is throwing this exception. MI checked maven dependencies to the deployment assembly and they are present in that location.I am a beginner to maven. If any suggestions please let me know.

网友答案:

Most common reason for these kind of errors is that you import multiple versions of same classes through Maven, either directly or through dependencies, so the wrong one is chosen from classpath in runtime. Or you imported some servlet classes already provided by Tomcat container. Try to exclude some of the artifacts from dependencies. Or here is servlet dependency I use, and it works without any problems with similar configuration as yours.

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>
分享给朋友:
您可能感兴趣的文章:
随机阅读: