简体   繁体   English

创建 org.springframework.web.servlet.LocaleResolver 类型的 Bean 时出现 BeanCreationException

[英]BeanCreationException when creating Bean of type org.springframework.web.servlet.LocaleResolver

I'm trying to apply internationalization in a Spring Boot app by following some guide.我正在尝试按照一些指南在 Spring Boot 应用程序中应用国际化。 For some reason, in the guide that I'm following it works but not on my local.出于某种原因,在我遵循的指南中它有效,但不适用于我的本地。 I can't figure out why.我不知道为什么。

The error I am getting in console is:我在控制台中遇到的错误是:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'localeResolver' org.springframework.beans.factory.BeanCreationException:创建名为“localeResolver”的bean时出错

import java.util.Locale;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

@SpringBootApplication
public class RestfulWebServicesApplication {

    public static void main(String[] args) {
        SpringApplication.run(RestfulWebServicesApplication.class, args);
    }
    
    @Bean
    public LocaleResolver localeResolver() {
        SessionLocaleResolver localeResolver = new SessionLocaleResolver();
        localeResolver.setDefaultLocale(Locale.US);
        return localeResolver();
    }
    
    @Bean
    public ResourceBundleMessageSource bundleMessageSource() {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setBasename("messages");
        return messageSource;
    }
}

I am aware that when Spring creates a Bean, it looks for a no-argument constructor.我知道当 Spring 创建一个 Bean 时,它会寻找一个无参数的构造函数。 Is there an alternative way to make this work where the @Bean definitions are in the class marked with @SpringBootApplication instead of a separate class marked with @Configuration ?如果@Bean定义位于标有@SpringBootApplication的class 而不是标有@Configuration的单独的class 中,是否有另一种方法可以使这项工作?

Thank you.谢谢你。

Edit: Below is a more detailed log info编辑:下面是更详细的日志信息

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-01-12 18:12:27.928 ERROR 5724 --- [  restartedMain] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'localeResolver' defined in com.in28minutes.rest.webservices.restfulwebservices.RestfulWebServicesApplication: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.LocaleResolver]: Factory method 'localeResolver' threw exception; nested exception is java.lang.StackOverflowError
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:658) ~[spring-beans-5.3.14.jar:5.3.14]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:486) ~[spring-beans-5.3.14.jar:5.3.14]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1352) ~[spring-beans-5.3.14.jar:5.3.14]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1195) ~[spring-beans-5.3.14.jar:5.3.14]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582) ~[spring-beans-5.3.14.jar:5.3.14]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.14.jar:5.3.14]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.14.jar:5.3.14]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.14.jar:5.3.14]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.14.jar:5.3.14]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.14.jar:5.3.14]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:953) ~[spring-beans-5.3.14.jar:5.3.14]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.14.jar:5.3.14]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.14.jar:5.3.14]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.6.2.jar:2.6.2]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:730) ~[spring-boot-2.6.2.jar:2.6.2]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:412) ~[spring-boot-2.6.2.jar:2.6.2]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:302) ~[spring-boot-2.6.2.jar:2.6.2]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1301) ~[spring-boot-2.6.2.jar:2.6.2]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1290) ~[spring-boot-2.6.2.jar:2.6.2]
    at com.in28minutes.rest.webservices.restfulwebservices.RestfulWebServicesApplication.main(RestfulWebServicesApplication.java:16) ~[classes/:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
    at java.base/java.lang.reflect.Method.invoke(Method.java:564) ~[na:na]
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) ~[spring-boot-devtools-2.6.2.jar:2.6.2]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.LocaleResolver]: Factory method 'localeResolver' threw exception; nested exception is java.lang.StackOverflowError
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.3.14.jar:5.3.14]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653) ~[spring-beans-5.3.14.jar:5.3.14]
    ... 24 common frames omitted
Caused by: java.lang.StackOverflowError: null

The return statement in your localeResolver() @Bean method is calling the method again: localeResolver() @Bean方法中的 return 语句再次调用该方法:

return localResolver();

This will result in a StackOverflowError as each call to localeResolver() will make another call to the same method.这将导致StackOverflowError ,因为每次调用localeResolver()都会再次调用同一方法。 Presumably, this is the cause of the BeanCreationException .据推测,这是BeanCreationException的原因。

I assume you want to return the localResolver local variable so the () should be removed:我假设您想返回localResolver局部变量,因此()应该被删除:

return localeResolver;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 org.springframework.beans.factory.BeanCreationException:org.springframework.beans.factory.BeanCreationException:创建bean时出错 - org.springframework.beans.factory.BeanCreationException:org.springframework.beans.factory.BeanCreationException: Error creating bean org.springframework.beans.factory.BeanCreationException:创建bean时出错 - org.springframework.beans.factory.BeanCreationException: Error creating bean org.springframework.beans.factory.BeanCreationException:在 Spring 中创建 bean 时出错 - org.springframework.beans.factory.BeanCreationException: Error creating bean in Spring org.springframework.beans.factory.BeanCreationException:创建名称为bean的错误 - org.springframework.beans.factory.BeanCreationException: Error creating bean with name org.springframework.beans.factory.BeanCreationException:创建名为“org.springframework.security.filterChains”的 bean 时出错: - org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.filterChains': 异常:org.springframework.beans.factory.BeanCreationException:创建名称为“ org.springframework.security.filterChains”的bean时出错 - Exception : org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.filterChains' 引起:org.springframework.beans.factory.BeanCreationException:创建名为“(inner bean)#7”的 bean 时出错:bean 实例化失败 - Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#7': Instantiation of bean failed 春季批处理:org.springframework.beans.factory.BeanCreationException:使用JobScope时创建名称为“ scopedTarget.writer”的bean时出错 - Spring batch : org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.writer' when using JobScope 创建名称为'org.springframework.web.servlet.view.ContentNegotiatingViewResolver#0'的bean时出错 - Error creating bean with name 'org.springframework.web.servlet.view.ContentNegotiatingViewResolver#0' 创建名为“org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping”的bean时出错 - Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM