当前位置: 首页 > 知识库问答 >
问题:

如何使用AnnotationConfigWebApplicationContext运行SpringApplication?

张鹏鹍
2023-03-14

我们有一个SpringApplication,它可以在默认的Application Context下运行,但是我们有一个场景,在这个场景中我们需要刷新上下文,而默认上下文不允许我们这样做。我已经更新了我们的主Application类,如下所示:

// package and import lines not shown here but are included in original source

@ComponentScan("edge")
@EnableAutoConfiguration
@Configuration
@EnableTransactionManagement
@EnableAsync
@EnableScheduling
public class Application {

    public static void main(String[] args) {

        SpringApplication app = new SpringApplication(Application.class);
        app.setApplicationContextClass(AnnotationConfigWebApplicationContext.class);
        app.run(args);
    }

使用此代码,调用应用程序。运行(args)会导致以下堆栈跟踪:

Exception in thread "main" java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext
at org.springframework.context.support.AbstractRefreshableApplicationContext.getBeanFactory(AbstractRefreshableApplicationContext.java:170)
at org.springframework.boot.context.event.EventPublishingRunListener.registerApplicationEventMulticaster(EventPublishingRunListener.java:70)
at org.springframework.boot.context.event.EventPublishingRunListener.contextPrepared(EventPublishingRunListener.java:65)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:305)
at edge.server.Application.main(Application.java:43)

逐步完成SpringApplication。run(),我注意到上下文的BeanFactory为null。如果我删除该行应用程序。setApplicationContextClass(AnnotationConfigWebApplicationContext.class),从而将应用程序设置为使用默认上下文,代码运行到调用refresh()的点。该调用导致:

java.lang.IllegalStateException: GenericApplicationContext does not support multiple refresh attempts: just call 'refresh' once

是否有人在SpringApplication中以我所描述的方式使用AnnotationConfigWebApplication Context并使其工作?如果是,您对使其工作有任何建议吗?我已经尝试在调用app.run()之前寻找手动创建BeanFactory的方法,但似乎没有任何公共方法可以做到这一点。提前感谢您提供的任何帮助!

编辑澄清:

感谢迄今为止的评论和回答。关于需要刷新ApplicationContext的场景以及我尝试使用AnnotationConfigWebApplicationContext的情况,我在最初的帖子中应该更加明确。我们有一些在服务器启动后运行的代码,用于备份和恢复,这涉及到修改我们正在使用的JpaRepositories的内容。我的理解是,在运行这段代码之后,我们需要刷新ApplicationContext,以便再次调用所有init方法。

尝试使用默认上下文类(AnnotationConfigEmbeddedWebApplication ationContext,GenericApplication ationContext的子类)执行此操作会抛出我之前提到的IllegalStateException(GenericApplication ationContext不支持多次刷新尝试:只需调用“刷新”一次)。这个异常促使我尝试将上下文显式设置为AnnotationConfigWebApplication ationContext,它是AbstractReFreshableApplication ationContext的子类。我的假设是我需要应用程序使用AbstractReFreshableApplication ationContext才能成功多次调用刷新方法。有没有办法让我上面的代码工作,或者我应该采取一些替代方法来允许我们多次刷新上下文?

共有1个答案

秦新立
2023-03-14

您正在使用Spring Boot,它已经为您做到了这一点,您只是让它变得过于复杂。将您的类更改为以下内容。

@EnableAsync
@EnableScheduling
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

并确保它位于edge软件包中,并确保您具有启用web应用程序的spring boot starter web依赖项。没有必要自己去搞乱上下文类。

 类似资料:
  • 我有一个Gradle项目,我正试图用它来运行Jetty。我的文件如下所示。 建筑格拉德尔 我试图从命令行运行这个项目,我使用的命令是: > 应构建项目,并输出以下内容: Jetty插件已被弃用,计划在Gradle 4.0中删除。考虑使用Gretty(https://github.com/akhikhl/gretty)而不是插件。在build_6xw4u3pr68h02k136x2vqowd。运行(

  • 当我尝试运行Phoenix的命令时,我得到 其他什么都没发生。我也无法让松鼠工作(当我单击“列表驱动程序”时,它会冻结)。 我没有将的类路径中,因为我不知道它指的是什么。 我使用的是HBase 0.98.6.1-Hadoop2、Phoenix 4.2.1和hadoop 2.2.0。

  • 问题内容: 我想用于一些Web测试,并且遇到了(https://github.com/detro/ghostdriver)。我已经按照自述文件中的说明进行了构建,并且可以在指定的端口上运行它,但是我不确定如何从Java代码访问Web驱动程序。为了澄清,我已经在ruby中看到了这个示例: 我只是不确定如何从java中执行相同操作。 问题答案: 我相信此链接将回答您的问题。您将需要Selenium 2

  • 我回到了JDK8和Jetty和SPDY,我看到现在Jetty 9.2支持ALPN协议而不是NPN协议(参见我的问题,如何在JDK8上运行Jetty和SPDY?)。所以我设置了: 但现在我有个例外: 我使用和jetty 9.2.2.v20140723。

  • 问题内容: 我尝试过的 然后 再发送密码导致没有root 然后导致“频道关闭”错误 然后导致不被root 然后写到标准输入并刷新它导致没有root 问题答案: 看看这个例子: 在此处找到具有更多说明的示例:http : //jessenoller.com/2009/02/05/ssh- programming-with-paramiko-completely- different/ 希望能帮助到你

  • 我正在尝试使用PySpark用Python运行Spark graphx。我的安装似乎是正确的,因为我能够很好地运行pyspark教程和(Java)GraphX教程。大概既然GraphX是Spark的一部分,pyspark应该可以接口它,对吗? 以下是Pyspark的教程:http://spark.apache.org/docs/0.9.0/quick-start.html http://spark