此文章為Spring Boot Reference Guide(2.1.5.RELEASE)的備忘錄。Chapter 8. Introducing Spring Boot
You can use Spring Boot to create a Java application that can be started by using java -jar or more traditional war deployments.
Spring Boot 2.1.5.RELEASE requires:
Java 8 and is compatible up to Java 11(included).
Spring Framework 5.1.6.RELEASE or above
Maven 3.3 or above
Chapter 10. Installing Spring Boot Installation Instruction for the Java Developer (Ways)Including the appropriate spring-boot-*.jar files on your classpath in the same way as any standard Java library.
Install appropriate Maven, make POM file inheriting from the spring-boot-starter-patent project and declare dependence spring-boot-starter-web. Optionally config Maven plugin to package the project as an executable jar file.
4.0.0 com.example myproject 0.0.1-SNAPSHOT org.springframework.boot spring-boot-starter-parent 2.1.5.RELEASE org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-maven-plugin
DEPENDENCIES AUTO UPGRATED
SPRING-BOOT-START-PARENT 1The spring-boot-start-parent provides a dependency-management section so that we can omit version tags for "blessed" dependences. When you upgrade Spring Boot itself, these dependencies are upgraded as well in a consistent way.
With this setup, we can also override individual dependencies by configure as below
Fowler-SR2
Install the Spring Boot CLI and run spring run app.groovy.
@RestController class ThisWillActuallyRun { @RequestMapping("/") String home() { "Hello World!" } }
Generate a new project structure by going to https://start.spring.io to shortcut steps.
Using scope=import dependencies as follows to keep the benefit of the dependency management(but not th e plugin management) and override individual dependencies.
org.springframework.data spring-data-releasetrain Fowler-SR2 pom import org.springframework.boot spring-boot-dependencies 2.1.5.RELEASE pom import
Upgrading from an Early Version of Spring Boot, you may need spring-boot-properties-migratorChapter 11. Developing First Spring Boot Application
Create a Maven pom.xml file and complete it, then run mvn package to test the working build.
mvn dependency:tree
src/main/java/Example.java
import org.springframework.boot.*; import org.springframework.boot.autoconfigure.*; import org.springframework.web.bind.annotation.*; @RestController @EnableAutoConfiguration // Tell Spring Boot to "guess" how you want to configure Spring based on the jar dependences. public class Example { @RequestMapping("/") String home() { return "Hello World!"; } public static void main(String[] args) { SpringApplication.run(Example.class, args); } }
Run mvn spring-boot:run to start the application.
Add the spring-boot-maven-plugin to out pom.xml to used to create an executable jar.
org.springframework.boot spring-boot-maven-plugin
MAMUAL OPERATIION REQUIRED
SPRING-BOOT-START-PARENT 2the spring-boot-starter-parent POM includes
configuration to bind the package goal. if you do not use the parent POM, you need to declare this configuration yourself. See https://docs.spring.io/spring-boot/docs/2.1.5.RELEASE/maven-plugin/usage.html for more detail.
run mvn package to create an executable jar.
run jave -jar target/myproject-0.0.1-SNAPSHOT.jar to launch the application.
You can use jar -tvf to peek inside.
EXAMPLESsome samples can be referenced at https://github.com/spring-projects/spring-boot/tree/v2.1.5.RELEASE/spring-boot-samples
DEPENDENCIES LISTChapter 13. Structuring Code Location of Mail Application ClassAll the spring modules that can use with Spring Boot, as well as refined third part libraries, can be found at https://github.com/spring-projects/spring-boot/blob/v2.1.5.RELEASE/spring-boot-project/spring-boot-dependencies/pom.xml
The @SpringBootApplication annotation is often placed on the main classpath, and it implicitly defines as base "search package" for certain items. Using a root package also allows the component scan to apply only on this project.
SpringBootApplication@SpringBootApplication == @EnableAutoConfiguration + @ComponentScan + @Configuration
Typical Layout:
com +- example +- myapplication +- Application.java | +- customer | +- Customer.java | +- CustomerController.java | +- CustomerService.java | +- CustomerRepository.java | +- order +- Order.java +- OrderController.java +- OrderService.java +- OrderRepository.javaChapter 15. Configuration Class
We generally recommend that the primary source be a single @Configuration class. Usually, the main class is a good candidate as the primary *@Configurations
@Import can be used to import additional configuration classes.
Alternatively, @ComponentScan can be used to automatically pick up all Spring component including @Configuration classes.
@ImportResource can be used to import load XML configurations file(Totally not recommended)
Chapter 16. Auto-configuration@EnableAutoConfiguration or @SpringBootApplication attempts to automatically configure application based on the jar dependencies. we generally recommend that you add one or the other to your primary @Configuration class only.
--DEBUG SWITCH WHEN START
To find out what auto-configuration is currently being applied.
We can define exclusion both at the annotation level and ny using the property.
Chapter 17. Spring Beans and Dependency Injection@ComponentsScan in maim class will automatically register @Component, @Service, @Repository, @Controller etc. as Spring Beans
Chapter 20. Developer ToolsProperty Defaultorg.springframework.boot spring-boot-devtools true
Developer tools are automatically disabled when running a fully packages. Applications launched from java -jar or started from a special class loader are considered as fully packages.
If that does not apply to you, try to exclude devtools or set the -Dspring.devtools.restart.enabled=false system property.
devtool propertiesAutomatic Restart
DevTools relies on the application context’s shutdown hook to close it during a restart. It does not work correctly if you have disabled the shutdown hook (SpringApplication.setRegisterShutdownHook(false)).
RESTART TECHNOLOGY
Two class loader, base and restart. Classes that do not change(for example third part jar) are loaded into base. Classes that are under development are loaded into restart. When restart, throw restart away. Take advantages of already available and populated base class loader.
Generally speaking, any project in IDE is loaded with the restart and any regular .jar file is loaded with base.
spring.devtools.restart.log-condition-evaluation-delta=false
Disable the logging of the report that shows the condition evaluation delta when restart triggered.
spring.devtools.restart.exclude=static/,public/
spring.devtools.restart.additional-exclude
spring.devtools.restart.additional-paths
spring.devtools.restart.enabled(when false, restart class loader still be initiated but does not watch for file change)
(System property)spring.devtools.restart.enabled(completely disable)
public static void main(String[] args) { System.setProperty("spring.devtools.restart.enabled", "false"); SpringApplication.run(MyApp.class, args); }
spring.devtools.restart.trigger-file
spring.devtools.livereload.enabled
LIVE RELOADGlobal Settings Remote Applicationtrigger browser refresh when a resource is changed.[http://livereload.com/extensi...]
Can be used in development environment.
Chapter 23 SpringApplicationFailureAnalyzers to handle error occur when failed to start.
If no analyzer was matched, you can use DEBUG mode when launching application to see detail info.
Adding a banner.txt file to class path.
Setting spring.banner.location property to locate an other file(spring.banner.charset).
Add banner.gif .pnf .jpg to class path.
Setting spring.banner.image.location
SpringApplication.setBanner, .org.springFramework.boot.banner interface and printBanner method.
Placeholders can be used.
MANIFEST.MF
spring.main.banner-mode: console, log, off
https://docs.spring.io/spring-boot/docs/2.1.5.RELEASE/api/org/springframework/boot/builder/SpringApplicationBuilder.html
Application Events and ListenersYou often need not use application events, bunt it is handy to know that they exist.
Web EnvironmentDetermine WebApplicationType:
If Spring MVC is present, an AnnotationConfigServletWebServerApplicationContext is used
If Spring MVC is not present and Spring WebFlux is present, an AnnotationConfigReactiveWebServerApplicationContext is used
Otherwise, AnnotationConfigApplicationContext is used
Changed by setWebApplicationType(WebApplicationType). setApplicationContextClass(…?).
Accessing Application Argumentsimport org.springframework.boot.*; import org.springframework.beans.factory.annotation.*; import org.springframework.stereotype.*; @Component public class MyBean { @Autowired public MyBean(ApplicationArguments args) { boolean debug = args.containsOption("debug"); Listfiles = args.getNonOptionArgs(); // if run with "--debug logfile.txt" debug=true, files=["logfile.txt"] } }
Or can be used by @Value annotation
Using Application Runner or CommandLineRunnerUsed when you want to run some special code once the SpringApplication has started.
Application Exit Admin Features Chapter 24 Externalized ConfigurationOverride order
~/.spring-boot-devtools.properties
Command line arguments. java -jar app.jar —ame=“Spring”. disabled by: SpringApplication.setAddCommandLineProperties(false)
SPRING_APPLICATION_JSON.
$ SPRING_APPLICATION_JSON="{"acme":{"name":"test"}}" && java -jar myapp.jar
java -Dspring.application.json="{"name":"test"}" -jar myapp.jar
java -jar myapp.jar --spring.application.json="{"name":"test"}
Java system properties
OS environment variables
application.properties
Name and path can be changed:
java -jar myproject.jar --spring.config.name=myproject.
java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties
Search order by default:
file:./config/
file:./
classpath:/config/
classpath:/
Usage: @Value("${name}")
Random Valuemy.secret=${random.value}
my.number=${random.int}
my.bignumber=${random.long}
my.uuid=${random.uuid}
my.number.less.than.ten=${random.int(10)}
my.number.in.range=${random.int[1024,65536]}
[To Be Continued]
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/75138.html
摘要:系列文章更新計劃列表主要對一些中常用的框架進行簡單的介紹及快速上手,外加相關資料的收集更新列表會不定期的加入新的內容以進行擴充,如果你對此感興趣可以站內聯系我。 導讀: 從第一次接觸Spring Boot 至今已經有半年多了,在這期間也瀏覽了許多和Spring Boot 相關的書籍及文章,公司里面的許多項目也一直在使用Spring Boot。關于Spring Boot的一些看法:Spr...
摘要:開發人員常用的框架文檔及中文翻譯,包含系列文檔,日志,,,,數據庫,,等最新官方文檔以及對應的中文翻譯。其它如果你有針對此網站好的建議或意見,也歡迎提更多的文檔和更多的文檔版本支持 開發人員常用的框架文檔及中文翻譯,包含 Spring 系列文檔(Spring, Spring Boot, Spring Cloud, Spring Security, Spring Session),日志(...
摘要:開發人員常用的框架文檔及中文翻譯,包含系列文檔,日志,,,,數據庫,,等最新官方文檔以及對應的中文翻譯。其它如果你有針對此網站好的建議或意見,也歡迎提更多的文檔和更多的文檔版本支持 開發人員常用的框架文檔及中文翻譯,包含 Spring 系列文檔(Spring, Spring Boot, Spring Cloud, Spring Security, Spring Session),日志(...
摘要:可以使用外部化配置來方便在不同環境的運行同樣的程序文件文件環境變量命令行參數內置順序實現了很多按以下順序進行合理的相同屬性的覆蓋目錄下的全局設置屬性,如果激活測試用例上的注解測試用例上的注解。 簡介 在應用中管理配置并不是一個容易的任務,尤其是在應用需要部署到多個環境中時。通常會需要為每個環境提供一個對應的屬性文件,用來配置各自的數據庫連接信息、服務器信息和第三方服務賬號等。通常的應用...
閱讀 3288·2021-09-08 09:45
閱讀 1251·2019-08-30 15:53
閱讀 1522·2019-08-30 14:12
閱讀 981·2019-08-29 17:01
閱讀 2568·2019-08-29 15:35
閱讀 394·2019-08-29 13:09
閱讀 1965·2019-08-29 12:32
閱讀 3083·2019-08-26 18:37