Project that build with Maven
Quick buile a Spring Boot project
Use your IDE and new a project with Spring Initializer
Some basic operations
Creat an excutable jar
To create an excutable jar we neen to add the spring-boot-maven-plugin
to our pom.xml.
Insert the following lines just below the dependencis
section
1 | <build> |
mvn package
1 | If you look in the `target` directory,you should see a JAR file. |
mvn spring-boot:run
1 |
|
Which is like the arbitration center of Spring Boot
###2. <dependencis>
This part is to import dependencis
starter
Starters are a set of convenient dependency descriptors that you can include in your application.
https://docs.spring.io/spring-boot/docs/2.1.8.RELEASE/reference/html/using-boot-build-systems.html#using-boot-starter
1 | <dependencies> |
Main procedure class,main entrance class
@SpringBootApplication
The @SpringBootApplication
annotation is often placed on your main class, and it implicitly defines a base “search package” for certain items.
For example, if you are writing a JPA application, the package of the @SpringBootApplication
annotated class is used to search for @Entity
items.
Using a root package also allows component scan to apply only on your project.
1 | @SpringBootApplication |
below is the detail of @SpringBootApplication,which is like a gather.
1 | @Target({ElementType.TYPE}) |
@SpringBootConfiguration
- @SpringBootConfiguration annotated class is an option class of Spring Boot.
@EnableAutoConfiguration
Spring Boot auto-configuration attempts to automatically configure your Spring application based on the jar dependencies that you have added.
You need to opt-in to auto-configuration by adding the @EnableAutoConfiguration
or @SpringBootApplication
annotations to one of your @Configuration classes.
(Because the @SpringBootConfiguration
have included @EnableAutoConfiguration,the auto-configuration wil automatically configure all component that in the same package with the @SpringBootConfiguration
)
Configuration file
Spring Boot default configuration in src/main/resources/.application.yml
or application.properties
1.YAML basic
format like Python
key:(space)value
1 | server: |
do not ignore case
2. key: value
Object,Map
key: value
1 | friends: |
Array
-value
1 | pets: |
inline
1 | pet: [dog,cat] |
@ConfigurationProperties(prefix = “person”)
Bind all properties in this class with the properties in configuration fileprefix = "person"
means bind class properties with person properties.
Only the component props can use the component function@ConfigurationProperties
.
1 |
|
@PropertySource(value = {“classpath:application2.yml’})
Like @ConfigurationProperties ,but this can select another configuration file.
@ImportResource
Import Spring configuration file,and
……..