6

I have an application with spring that use a JpaConfiguration class to deal with my database, and a WebAppMvcConfigurer class to deal with the front via json message. Both have an @Configuration and are in the same package. I have an App class in a root package with @Configuration and @ComponentScan with my main method.

When I launch App class I get this error :

    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

What "works" so far is delete one of the two @Configuration in either JpaConfiguration or WebAppMvcConfigurer. If i delete the @Configuration WebApp one i can interact with my database no problem, but cant access anything from my angular application. If I delete the @Configuration in JpaConf the front part of the application works and I can access the json my Java application sends with my angular application no problem, but cant do anything with my database.

Somewhere I read to change @Configuation to @WebAppConfiguration in WebAppMvcConfigurer, it does the same as removing the @Configuration, JpaConfiguration works fine and front part doesnt work.

I checked dependencies and tried the last version without success. I tried to change in App : @ComponentScan(basePackages = { "org.example.springproject" }, excludeFilters = { @Filter(type = FilterType.ANNOTATION, value = Configuration.class) }) without success too, it is the same as deleting both @Configuration in Jpa and webapp

My app class :

package com.bnpp.creditauto;

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

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

JpaConfiguration class :

package com.bnpp.creditauto.config;

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

WebAppMvcConfigurer class :

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();
    }
}
Brice B
  • 103
  • 1
  • 1
  • 8

2 Answers2

2

So I found the answer, you can't launch the main method with a spring context when you have a @Configuration in a WebMvcConfigurer class and @Configuration in a class handling your Jpa configuration. If you remove either config you can use your main method but the class without @Configuration will stop working. To create a method that initializes or tests and call it with HTTP request in a browser or your application dealing with the front part and it will work fine.

Jakov
  • 879
  • 2
  • 17
  • 36
Brice B
  • 103
  • 1
  • 1
  • 8
  • can you explain a bit clearer ... maybe using an example – Zeinab Ghaffarnasab Oct 04 '20 at 11:50
  • []https://github.com/BriceBKOH2/CreditAutoApp/tree/master/credit_auto_Java/src/main/java/com/bnpp/creditauto/config it has been so long and i dont code with spring anymore. I have my three classes that does the configuration here, i hope it helps. Apparenlty i created an WebAppInitializer that extends WebApplicationInitialized, check the code in the file. – Brice B Oct 05 '20 at 13:21
  • tnx for the answer, I will check it out – Zeinab Ghaffarnasab Oct 10 '20 at 06:24
1

Thought of adding another scenario if someone cant still find the issue. So with the annotation "@SpringBootApplication" spring takes care most of the sub annotations like "@ComponentScan". let's say you have a test config class and you give your own component scan packages, then the default application level context loading wont happen. so in this case you only want to stick with other configuration like injecting your own property files with datasources (mem dbs)

Juliyanage Silva
  • 2,529
  • 1
  • 21
  • 33