gpt4 book ai didi

java - 创建名称为 'resourceHandlerMapping' 的 bean 时出错

转载 作者:太空宇宙 更新时间:2023-11-04 09:14:24 26 4
gpt4 key购买 nike

我有一个带有 spring 的应用程序,它使用 JpaConfiguration 类来处理我的数据库,并使用 WebAppMvcConfigurer 类通过 json 消息处理前端。两者都有一个@Configuration并且位于同一个包中。我的根包中有一个 App 类,其中包含 @Configuration 和 @ComponentScan 以及我的 main 方法。

当我启动应用程序类时,我收到此错误:

    Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'resourceHandlerMapping' defined in class path resource [org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'resourceHandlerMapping' threw exception; nested exception is java.lang.IllegalStateException: No ServletContext set
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:656)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:636)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1338)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:879)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:89)
at com.bnpp.creditauto.App.main(App.java:22)
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'resourceHandlerMapping' threw exception; nested exception is java.lang.IllegalStateException: No ServletContext set
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:651)
... 14 more
Caused by: java.lang.IllegalStateException: No ServletContext set
at org.springframework.util.Assert.state(Assert.java:73)
at org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.resourceHandlerMapping(WebMvcConfigurationSupport.java:533)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
... 15 more

到目前为止“有效”的是删除 JpaConfiguration 或 WebAppMvcConfigurer 中的两个 @Configuration 之一。如果我删除 @Configuration WebApp,我可以毫无问题地与数据库交互,但无法从我的角度应用程序访问任何内容。如果我删除 JpaConf 中的 @Configuration,应用程序的前部可以正常工作,并且我可以访问我的 Java 应用程序通过我的角度应用程序发送的 json,没有问题,但无法对我的数据库执行任何操作。

我读到在WebAppMvcConfigurer中将@Configuration更改为@WebAppConfiguration,它的作用与删除@Configuration相同,JpaConfiguration工作正常,前面部分不起作用。

我检查了依赖项并尝试了最后一个版本,但没有成功。我尝试在应用程序中进行更改: @ComponentScan(basePackages = { "org.example.springproject"}, exceptFilters = { @Filter(type = FilterType.ANNOTATION, value = Configuration.class) })也不成功,就相当于把Jpa和webapp中的@Configuration都删除了

我的应用程序类:

package com.bnpp.creditauto;

@Configuration
@ComponentScan//("com.bnpp.creditauto")
public class App {

public static void main(String[] args) {
...
}

Jpa配置类:

package com.bnpp.creditauto.config;

@Configuration
@EnableTransactionManagement
@PropertySource("classpath:application.properties")
public class JpaConfiguration {
...
}

WebAppMvcConfigurer 类:

package com.bnpp.creditauto.config;

@Configuration
@EnableWebMvc
public class WebAppMvcConfigurer implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/css/**").addResourceLocations("/css/");
}

@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}

最佳答案

所以我找到了答案,当您在 WebMvcConfigurer 类中具有 @Configuration 以及在处理 Jpa 配置的类中具有 @Configuration 时,您无法使用 spring 上下文启动 main 方法。如果删除任一配置,您可以使用 main 方法,但没有 @Configuration 的类将停止工作。创建一个方法来初始化或测试并在浏览器或处理前面部分的应用程序中使用 HTTP 请求调用它,它将正常工作。

关于java - 创建名称为 'resourceHandlerMapping' 的 bean 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59249591/

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