创建com.luv2code.springsecurity.demo.config.DemoAppConfig中定义的名称为'viewResolver'的bean时出错

时间:2020-08-18 08:27:29

标签: java spring maven spring-mvc javabeans

我在上下文初始化方面遇到问题。我不使用xml配置文件。我想要没有xml的spring配置,但是出现错误。请帮忙解决。

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'viewResolver' defined in com.luv2code.springsecurity.demo.config.DemoAppConfig: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.ViewResolver]: Factory method 'viewResolver' threw exception; nested exception is java.lang.StackOverflowError
    org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:625)
    org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:456)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1287)

我的DemoAppConfig

package com.luv2code.springsecurity.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.luv2code.springsecurity.demo")
public class DemoAppConfig {
    
    @Bean
    public ViewResolver viewResolver() {
        
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        
        viewResolver.setPrefix("/WEB-INF/view/");
        viewResolver.setSuffix(".jsp");
        
        return viewResolver();
    }
    
}

我的WebInit

public class MySpringMvcDisptacherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        // TODO Auto-generated method stub
        return new Class[] {DemoAppConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        // TODO Auto-generated method stub
        return new String[] { "/" };
    }

}

我知道我的问题出在DemoAppConfig中,但我根本找不到它。它必须与解析器一起使用。 但是我没有得到想要的结果。

2 个答案:

答案 0 :(得分:0)

viewResolver()方法会自行调用。这将永远不会结束,直到堆栈(跟踪方法调用)的容量用完为止-“堆栈溢出”。

有关出色的深入说明,请参见the answer to "What is a StackOverflowError?"

我想您不想返回viewResolver(),而是想返回局部变量viewResolver(请注意,此处没有括号)。

答案 1 :(得分:0)

尝试返回 返回viewResolver; 代替 返回viewResolver();

@Configuration
@EnableWebMvc
@ComponentScan(basePackages="com.luv2code.springsecurity.demo")
public class DemoAppConfig {

    // define a bean for ViewResolver

    @Bean
    public ViewResolver viewResolver() {
        
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        
        viewResolver.setPrefix("/WEB-INF/view/");
        viewResolver.setSuffix(".jsp");
        
        return viewResolver;
    }
    
}