我有一个带有spring的应用程序,它使用JpaConfiguration类处理我的数据库,并使用WebAppMvcConfigurer类通过json消息处理前端。两者都有一个@Configuration并且都在同一个包中。我的主方法的根包中有一个@AppConfiguration和@ComponentScan的App类。
启动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中的两个@Configuration之一。如果我删除@Configuration WebApp,那么我可以与我的数据库进行交互,但是不能从我的角度应用程序访问任何内容。 如果删除JpaConf中的@Configuration,则该应用程序的前部工作正常,并且可以访问我的Java应用程序与我的angular应用程序发送的json没问题,但不能对我的数据库做任何事情。
在某些地方,我读到了将@Configuation更改为WebAppMvcConfigurer中的@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();
}
}
答案 0 :(得分:1)
如果有人仍然无法找到问题,则考虑添加另一个场景。因此,使用注释“@SpringBootApplication”,spring 会处理大部分子注释,例如“@ComponentScan”。假设您有一个测试配置类并且您提供自己的组件扫描包,那么默认的应用程序级别上下文加载将不会发生。因此,在这种情况下,您只想坚持使用其他配置,例如使用数据源 (mem dbs) 注入您自己的属性文件
答案 1 :(得分:0)
因此,如果找到了答案,则当WebMvcConfigurer类中的@Configuration和处理Jpa配置的类中的@Configuration时,您无法使用spring上下文启动main方法。 如果删除这两个配置,则可以使用主方法,但没有@Configuration的类将停止工作。因此,创建一个初始化或测试方法,并在浏览器或处理前端的应用程序中使用httprequest进行调用,即可正常工作。