英文:
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 '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)
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 = "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();
}
}
my 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[] { "/" };
}
}
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="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;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论