当前位置: 首页 > 知识库问答 >
问题:

使用spring-boot配置自己的消息源

壤驷康裕
2023-03-14

我使用的是spring-boot,使用的是MessageSourceAutoConfiguration,但现在对于messageSource,我想使用ResourceBundleMessageSource定义一个ParentMessageSource。但是,我看不出如何用引导的自动配置来做到这一点。我已经尝试排除MessageSourceAutoConfiguration.class,但在尝试定义自己的MessageSourcebean时遇到以下异常
请提供任何帮助。

2015-07-20 09:33:31,811 [BOF37942::] ERROR org.springframework.boot.SpringApplication - Application startup failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'defaultServletHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'defaultServletHandlerMapping' threw exception; nested exception is java.lang.IllegalArgumentException: A ServletContext is required to configure default servlet handling
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1119) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1014) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757) ~[spring-context-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480) ~[spring-context-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.2.5.RELEASE.jar:1.2.5.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686) [spring-boot-1.2.5.RELEASE.jar:1.2.5.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:320) [spring-boot-1.2.5.RELEASE.jar:1.2.5.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:957) [spring-boot-1.2.5.RELEASE.jar:1.2.5.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:946) [spring-boot-1.2.5.RELEASE.jar:1.2.5.RELEASE]
    at com.lv.gi.br.Application.main(Application.java:31) [main/:na]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'defaultServletHandlerMapping' threw exception; nested exception is java.lang.IllegalArgumentException: A ServletContext is required to configure default servlet handling
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    ... 17 common frames omitted
Caused by: java.lang.IllegalArgumentException: A ServletContext is required to configure default servlet handling
    at org.springframework.util.Assert.notNull(Assert.java:112) ~[spring-core-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer.(DefaultServletHandlerConfigurer.java:53) ~[spring-webmvc-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.defaultServletHandlerMapping(WebMvcConfigurationSupport.java:426) ~[spring-webmvc-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$EnableWebMvcConfiguration$$EnhancerBySpringCGLIB$$e80e61d6.CGLIB$defaultServletHandlerMapping$32() ~[spring-core-4.1.7.RELEASE.jar:1.2.5.RELEASE]
    at org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$EnableWebMvcConfiguration$$EnhancerBySpringCGLIB$$e80e61d6$$FastClassBySpringCGLIB$$3900e0ca.invoke() ~[spring-core-4.1.7.RELEASE.jar:1.2.5.RELEASE]
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) ~[spring-core-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:309) ~[spring-context-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$EnableWebMvcConfiguration$$EnhancerBySpringCGLIB$$e80e61d6.defaultServletHandlerMapping() ~[spring-core-4.1.7.RELEASE.jar:1.2.5.RELEASE]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_51]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[na:1.7.0_51]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_51]
    at java.lang.reflect.Method.invoke(Method.java:606) ~[na:1.7.0_51]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    ... 18 common frames omitted

共有1个答案

云韬
2023-03-14

我在WebFlowConfig@Configuration类中定义了'message source'和'parent messageSource'@bean。我将这两个@bean移到它们自己的MessageConfig@configuration类中,结果一切正常!我的灵感来自一个类似的问题:如何将基于方法的安全性添加到Spring Boot项目中?附说明:

“试着把...”{在这里插入bean/s}“...在一个单独的@configuration类中。您似乎在迫使它在ServletContext就绪之前被实例化...”

 类似资料:
  • 我正在使用Spring Cloud Stream和RabbitMQ活页夹。它可以很好地处理字节[]负载和Java本机序列化,但我需要处理JSON负载。 这是我的处理器类。 输入到和输出到是带有Jackson注释的POJO。 如何配置JSON转换策略 消息头应该如何被接受和处理

  • 我正在使用Spring-Boot(1.5.9-release)构建一个应用程序,我希望将log4j2与它一起使用。 我想配置log4j2以使用JDBC连接记录到数据库表。 我的问题是我无法配置log4j2来使用我为Spring-Boot配置的数据源。 我的问题是我无法从Spring-Boot获得DataSource的实例。 我对依赖注入不是很熟悉,但我假设由于我需要Spring-Boot来注入数据

  • 本文向大家介绍Spring Boot使用和配置Druid,包括了Spring Boot使用和配置Druid的使用技巧和注意事项,需要的朋友参考一下 1、引入依赖包 2、配置application.properties 3、目前Spring Boot中默认支持的连接池有dbcp,dbcp2, tomcat, hikari三种连接池。 由于Druid暂时不在Spring Bootz中的直接支持,故需要

  • 我正试图用我的Spring boot应用程序配置Swagger UI。尽管似乎加载正确,但是不加载带注释的REST API。 这是我所拥有的: pom.xml: 昂首阔步。JAVA : 代码检查中的中的错误:。 我谷歌了一下(也尝试了网络配置mvc),但是错误仍然存在。也许我在. iml文件中丢失了一个资源引用?

  • 在像这样的典型Spring Boot应用程序中,我们如何配置它来使用“自定义”日志配置? 例如,在我的应用程序运行的当前环境中,日志正在导致错误,我如何使用其他日志例如: