【问题标题】:Spring bean context refresh failingSpring bean上下文刷新失败
【发布时间】:2017-12-15 14:31:15
【问题描述】:

我正在尝试执行以下代码并遇到一些错误。

AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
        ctx.register(AspectConfig.class);
        try{
            ctx.refresh();
        }catch(Exception e){
            ctx.close();
            throw new RuntimeException("Bean refresh failed with exception:" + e.getMessage());
        }

我的AspectConfig.java 看起来像

@EnableWebMvc
@Configuration
@EnableAspectJAutoProxy
@ComponentScan(basePackages={"ac","ab","az"})
public class AspectConfig {
     public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
            configurer.enable();
        }
}

所以这里主要是我使用spring mvc call来调用代码sn-p。我的春天WebServletConfiguration如下

@Override
    public void onStartup(ServletContext ctx) throws ServletException {
        AnnotationConfigWebApplicationContext webCtx = new AnnotationConfigWebApplicationContext();
        webCtx.register(AppConfig.class);
        webCtx.setServletContext(ctx);
        ServletRegistration.Dynamic servlet = ctx.addServlet("dispatcher", new DispatcherServlet(webCtx));
        servlet.setLoadOnStartup(1);
        servlet.addMapping("/");
    }

我的AppConfigAspectConfig 几乎相同,只是它包含ViewResolver 的定义。

错误是

Exception in thread "pool-1-thread-2" java.lang.RuntimeException: Bean refresh failed with exception:Error creating bean with name 'resourceHandlerMapping' defined in class path resource [org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'resourceHandlerMapping' threw exception; nested exception is java.lang.IllegalStateException: No ServletContext set
    at framework.AnnotationProcessor.process(AnnotationProcessor.java:40)
    at runners.MultiTestRunnable.run(MultiTestRunnable.java:29)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

我查看了下面的链接,但似乎都没有成功。是不是我不能用AnnotationContextSprings MVC

以下是我看过的链接

Spring 5.0.0.M3 Error calling ApplicationEventListener : No ServletContext set - Exception encountered during context initialization

Error creating bean with name 'resourceHandlerMapping' defined in class path resource [EnableWebMvcConfiguration.class]

【问题讨论】:

  • 我们可以看看hte堆栈跟踪吗?
  • @YassineBadache :我已经更新了堆栈跟踪。

标签: java spring spring-mvc spring-boot


【解决方案1】:

为什么需要两个独立的上下文? 可以在AppConfig类中添加@EnableAspectJAutoProxy注解试试吗?

否则,如果您需要单独的 web 和根上下文,那么我建议您尝试使用 AbstractAnnotationConfigDispatcherServletInitializer 类进行配置,这可以替代您的 WebServletConfiguration 类。 详情请参考link

【讨论】:

  • 此外,在这种情况下,您必须使用“AnnotationConfigWebApplicationContext”,因为这是一个 Web 应用程序。我不认为 AnnotationConfigApplicationContext 在这种情况下会有用。
猜你喜欢
  • 1970-01-01
  • 2017-08-24
  • 2022-12-06
  • 2017-03-17
  • 2014-09-13
  • 1970-01-01
  • 2012-05-14
  • 1970-01-01
  • 2013-03-12
相关资源
最近更新 更多