Error creating bean with name 'viewResolver' defined in com.luv2code.springsecurity.demo.config.DemoAppConfig

huangapple go评论84阅读模式
英文:

Error creating bean with name 'viewResolver' defined in com.luv2code.springsecurity.demo.config.DemoAppConfig

问题

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

org.springframework.beans.factory.BeanCreationException: 在 com.luv2code.springsecurity.demo.config.DemoAppConfig 中定义的名为 'viewResolver' 的 Bean 创建失败;通过工厂方法进行实例化时失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [org.springframework.web.servlet.ViewResolver]:工厂方法 'viewResolver' 抛出异常;嵌套异常是 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() {
		return null;
	}

	@Override
	protected Class<?>[] getServletConfigClasses() {
		return new Class[] {DemoAppConfig.class};
	}

	@Override
	protected String[] getServletMappings() {
		return new String[] { "/" };
	}

}

我知道问题出在 DemoAppConfig 中,但我根本无法找到它。它一定与 Resolver 有关。但我没有得到我想要的结果。
英文:

I had problems with context initialization. I don't use xml-configuration files. I've wanted to make spring configuration with no xml, but error appears. Please help to solve.

org.springframework.beans.factory.BeanCreationException: Error creating bean with name &#39;viewResolver&#39; 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 &#39;viewResolver&#39; 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)

My 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 = &quot;com.luv2code.springsecurity.demo&quot;)
public class DemoAppConfig {
	
	@Bean
	public ViewResolver viewResolver() {
		
		InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
		
		viewResolver.setPrefix(&quot;/WEB-INF/view/&quot;);
		viewResolver.setSuffix(&quot;.jsp&quot;);
		
		return viewResolver();
	}
	
}

my WebInit

public class MySpringMvcDisptacherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

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

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

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

}

I know my problem is in DemoAppConfig but i am simply not able to find it. It has to be with Resolver.
But i am not getting the result i want.

答案1

得分: 2

viewResolver() 方法会调用自身。这将永远不会结束,直到堆栈(用于跟踪方法调用)耗尽容量 - 即发生“堆栈溢出”。

有关详细解释,请参阅“什么是 StackOverflowError?”的答案。

与其再次调用 viewResolver(),我认为你想要返回局部变量 viewResolver(注意,这里不需要括号)。

英文:

The viewResolver() method invokes itself. This will never end, until the stack (which keeps track of method invocations) runs out of capacity - the "stack overflows".

See the answer to "What is a StackOverflowError?" for an excellent in-depth explanation.

Instead of invoking viewResolver() again, I think you want to return the local variable viewResolver (take care, no brackets here).

答案2

得分: 0

尝试返回

return viewResolver;

而不是

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

    // 定义一个 ViewResolver 的 bean

    @Bean
    public ViewResolver viewResolver() {

        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();

        viewResolver.setPrefix("/WEB-INF/view/");
        viewResolver.setSuffix(".jsp");

        return viewResolver;
    }

}
英文:

Try to return
return viewResolver;
instead of
return viewResolver();

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

	// define a bean for ViewResolver

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

huangapple
  • 本文由 发表于 2020年8月18日 16:27:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/63464693.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定