如何将自定义ApplicationContextInitializer添加到Spring Boot应用程序? [英] How to add custom ApplicationContextInitializer to a spring boot application?

查看:131
本文介绍了如何将自定义ApplicationContextInitializer添加到Spring Boot应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将自定义ApplicationContextInitializer添加到Spring Web应用程序的一种方法是将其添加到web.xml文件中,如下所示.

One way to add custom ApplicationContextInitializer to spring web application is to add it in the web.xml file as shown below.

<context-param>
    <param-name>contextInitializerClasses</param-name>
    <param-value>somepackage.CustomApplicationContextInitializer</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

但是,由于我使用的是Spring Boot,是否有任何方法不必创建web.xml来添加CustomApplicationContextInitializer?

But since I am using spring boot, is there any way I don't have to create web.xml just to add CustomApplicationContextInitializer?

推荐答案

您可以在META-INF/spring.factories

org.springframework.context.ApplicationContextInitializer=\
com.example.YourInitializer

您还可以在运行前将它们添加到SpringApplication

You can also add them on your SpringApplication before running it

application.addInitializers(YourInitializer.class);
application.run(args);

或者在构建器上

new SpringApplicationBuilder(YourApp.class)
    .initializers(YourInitializer.class);
    .run(args);

乍看之下从文档中看不出来,所以我打开了#5091 检查.

It wasn't obvious from the doc at first glance so I opened #5091 to check.

这篇关于如何将自定义ApplicationContextInitializer添加到Spring Boot应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆