創建非Web應用程序

 

目錄

官方文檔

71.5 Create a non-web application

翻譯

71.5 創建非Web應用程序


官方文檔

71.5 Create a non-web application

Not all Spring applications have to be web applications (or web services). If you want to execute some code in a main method, but also bootstrap a Spring application to set up the infrastructure to use, then it’s easy with the SpringApplication features of Spring Boot. A SpringApplication changes its ApplicationContext class depending on whether it thinks it needs a web application or not. The first thing you can do to help it is to just leave the servlet API dependencies off the classpath. If you can’t do that (e.g. you are running 2 applications from the same code base) then you can explicitly call setWebEnvironment(false) on your SpringApplication instance, or set the applicationContextClass property (through the Java API or with external properties). Application code that you want to run as your business logic can be implemented as a CommandLineRunner and dropped into the context as a @Bean definition.

翻譯

71.5 創建非Web應用程序

並不是所有的Spring應用都必須是Web應用(或者 WebService)。如果你想在main方法執行一些代碼,但是也需要引導Spring應用設置基礎框架。然而使用SpringBoot的SpringApplication特性就變得容易多了。一個SpringApplication 改變其上下文ApplicationContext 根據他是否是web應用。第一件事情你可以做的就是把servlet API的依賴從根路徑移除掉。如果不能這麼做(比如兩個應用共用一段基礎代碼)那麼你可以使用 SpringApplication實例設置setWebEnvironment(false) 或者設置applicationContextClass 屬性(通過java API或者外部屬性文件配置)。希望運行自己業務邏輯的應用程序代碼可以實現CommandLineRunner然後通過@Bean放入Context(上下文)中。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章