gpt4 book ai didi

java - 运行Spring MVC项目时出错

转载 作者:行者123 更新时间:2023-12-02 01:47:51 25 4
gpt4 key购买 nike

我以前从未遇到此错误。错误是某种豆刷新似乎没有发生。我在其他项目中也拥有与下面相同的文件,并且工作正常。但是,当我用相同的文件创建一个新项目并尝试在maven安装后运行时。它不起作用。

我已经尝试过刷新项目,并再次进行行家清理和安装,甚至清理Tomecat Work Directory,以确保以前的运行缓存不存在。但是,似乎没有任何效果。

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>EduYouth</display-name>
<welcome-file-list>
<welcome-file>/jsp/Welcome.jsp</welcome-file>

</welcome-file-list>
<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>


<listener>

<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
</web-app>


调度程序

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">


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

<context:property-placeholder ignore-unresolvable="true" location="classpath:config.properties" />
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" p:sessionFactory-ref="sessionFactory" />


<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" p:packagesToScan="com.model">
<property name="dataSource" ref="dataSource"/>


<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>

</props>
</property>

</bean>

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="mediaTypes">
<map>
<entry key="json" value="application/json" />
<entry key="xml" value="text/xml" />
</map>
</property>
<property name="defaultContentType" value="text/html" />
<property name="defaultViews">
<list>
<bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" />
</list>
</property>
</bean>


<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.url}"
p:username="${jdbc.username}"
p:password="${jdbc.password}"/>
<mvc:annotation-driven/>

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

<property name="prefix" value="/jsp/" />

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


</bean>
</beans>


applicationContext.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">


<mvc:annotation-driven />

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="mediaTypes">
<map>
<entry key="json" value="application/json" />
<entry key="xml" value="text/xml" />
</map>
</property>
<property name="defaultContentType" value="text/html" />
<property name="defaultViews">
<list>
<bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" />
</list>
</property>
</bean>

<!-- Enable annotation driven controllers, validation etc... -->


<mvc:resources location="/images/" mapping="/images/**"/>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="prefix" value="/jsp/" />

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

</bean>




</beans>


错误

  > 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:170)
> at
> org.springframework.context.support.AbstractApplicationContext.destroyBeans(AbstractApplicationContext.java:921)
> at
> org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:895)
> at
> org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:841)
> at
> org.springframework.web.context.ContextLoader.closeWebApplicationContext(ContextLoader.java:579)
> at
> org.springframework.web.context.ContextLoaderListener.contextDestroyed(ContextLoaderListener.java:115)
> at
> org.apache.catalina.core.StandardContext.listenerStop(StandardContext.java:5157)
> at
> org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5830)
> at
> org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:221)
> at
> org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:149)
> at
> org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1694)
> at
> org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1684)
> at java.util.concurrent.FutureTask.run(FutureTask.java:266) at
> java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
> at
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
> at java.lang.Thread.run(Thread.java:748


最佳答案

经过大量的清理工作并用Tomcat Work Directory的清理方法重新安装了maven plus之后,也许这不是导致上述错误的原因。

最后,它在更改<contex-param>中的值后起作用。我不明白为什么它会像上一次运行一样默认在类路径下获取资源。我在上下文路径中另外添加了resources

下面的添加/resources/..修复程序起作用,因为该文件夹位于src下,并且以前也可以正常工作。

 <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:resources/applicationContext.xml</param-value> </context-param>

关于java - 运行Spring MVC项目时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57445392/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com