23.3 Customizing SpringApplication

如果默认的SpringApplication不符合你的口味,你可以创建一个本地实例(不使用SpringApplication.run 静态方法)并对它进行自定义。例如,想要关闭banner你可以这样写:

public static void main(String[] args) {
    SpringApplication app = new SpringApplication(MySpringConfiguration.class);
    app.setBannerMode(Banner.Mode.OFF);
    app.run(args);
}

传递给SpringApplication的构造器参数将作为spring beans的配置源,多数情况下,它们是一些@Configuration类的引用,但也可能是XML配置(org.springframework.core.io.Resource)或要扫描包(java.lang.Package)的引用。 SpringApplications can read beans from a variety of different sources. It is generally recommended that a single @Configuration class is used to bootstrap your application, however, any of the following sources can also be used:

  • Class- A Java class to be loaded by AnnotatedBeanDefinitionReader
  • Resource - An XML resource to be loaded by XmlBeanDefinitionReader, or a groovy script to be loaded by GroovyBeanDefinitionReader
  • Package - A Java package to be scanned by ClassPathBeanDefinitionScanner
  • CharSequence - A class name, resource handle or package name to loaded as appropriate. If the CharSequence cannot be resolved to class and does not resolve to a Resource that exists it will be considered a Package. -----来自(Spring Boot Docs 1.5.7.RELEASE API) - Spring IO

你也可以使用application.properties文件来配置SpringApplication,具体参考24. 外部配置(Externalized Configuration),访问SpringApplication Javadoc可获取完整的配置选项列表。

results matching ""

    No results matching ""