web-dev-qa-db-ja.com

「resourceHandlerMapping」という名前のBeanの作成エラー

JpaConfigurationクラスを使用してデータベースを処理するSpringのアプリケーションと、jsonメッセージを介してフロントを処理するWebAppMvcConfigurerクラスがあります。どちらにも@Configurationがあり、同じパッケージに含まれています。ルートパッケージにAppクラスがあり、@ Configurationと@ComponentScanをmainメソッドで使用しています。

Appクラスを起動すると、次のエラーが発生します。

_    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の2つの@Configurationのうちの1つを削除することです。 @Configuration WebAppを削除すると、データベースと問題なく対話できますが、angularアプリケーションからは何にもアクセスできません。JPaConfで@Configurationを削除すると、アプリケーションの前部が機能します私はjson my Javaアプリケーションがmy angularアプリケーションで問題なく送信することにアクセスできますが、データベースで何もできません。

WebAppMvcConfigurerで@Configuationを@WebAppConfigurationに変更するために読んだどこかで、@ Configurationを削除するのと同じように動作し、JPaConfigurationは正常に機能し、フロントパーツは機能しません。

依存関係を確認し、最後のバージョンを試したが成功しなかった。私もAppで変更しようとしました:@ComponentScan(basePackages = { "org.example.springproject" }, excludeFilters = { @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) {
        ...
}
_

JpaConfigurationクラス:

_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();
    }
}
_
3
Brice B

そのため、答えが見つかった場合、WebMvcConfigurerクラスに@Configurationがあり、Jpa構成を処理するクラスに@Configurationがあると、春のコンテキストでmainメソッドを起動できません。どちらかの構成を削除すると、メインメソッドを使用できますが、@ Configurationのないクラスは機能しなくなります。したがって、初期化またはテストするメソッドを作成し、ブラウザまたはフロントパートを処理するアプリケーションでhttprequestを使用して呼び出すと、正常に動作します。

1
Brice B