Spring boot document Study Notes day01 and Study Notes day01
1.Use spring-boot-starter-parent in pom. xml:
Maven users can inherit from the spring-boot-starter-parent project to obtain sensible defaults. The parent project provides the following features:
- Java 1.8 as the default compiler level.
- UTF-8 source encoding.
- A Dependency Management section, inherited from the spring-boot-dependencies pom, that manages the versions of common dependencies. this dependency management lets you omit <version> tags for those dependencies when used in your own pom.
- Sensible resource filtering.
- Sensible plugin configuration (exec plugin, Git commit ID, and shade ).
- Sensible resource filtering for application. properties and application. yml including profile-specific files (for example, application-dev.properties and application-dev.yml)
2. pom. xmlWhich of the following configurations are available:
Https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-dependencies/pom.xml
3.It is best to place the startup class in the package of the outermost layer:
We generally recommend that you locate your main application class in a root package abve other classes. the @ EnableAutoConfiguration 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 @ EnableAutoConfiguration annotated class is used to search for @ Entity items.
Using a root package also lets the @ ComponentScan annotation be used without needing to specify a basePackage attribute. You can also use the @ SpringBootApplication annotation if your main class is in the root package.
The following listing shows a typical layout:
Com
+-Example
+-Myapplication
+-Application. java
|
+-Customer
| +-Customer. java
| +-CustomerController. java
| +-CustomerService. java
| +-CustomerRepository. java
|
+-Order
+-Order. java
+-OrderController. java
+-OrderService. java
+-OrderRepository. java
4.Use java configuration whenever possible to avoid xml configuration
You need not put all your@ConfigurationInto asingle class.@ImportAnnotation can be used to import additional configuration classes. Alternatively, you can use@ComponentScanTo automatically pick up all Spring components, including@ConfigurationClasses.
If you absolutely must use XML based configuration, we recommendthat you still start with@ConfigurationClass. You can then usean@ImportResourceAnnotation to load XML configuration files.
5.Automatic Configuration
You need to opt-in to auto-configuration byadding@EnableAutoConfigurationOr@SpringBootApplicationAnnotations to one of your@ConfigurationClasses., just add to one
6.Exclude classes that do not require automatic configuration
@ EnableAutoConfiguration (exclude = {performanceautoconfiguration. class })
Or
You can also control the list of auto-configuration classes to exclude by usingspring.autoconfigure.excludeProperty.
7. BeanInjection container
If you structure your code as suggested above (locating your application class in a root package), you can add@ComponentScanWithout any arguments. All of your application components (@Component,@Service,@Repository,@ControllerEtc.) are automatically registered as Spring Beans.
8. @ SpringBootApplication replaces several commonly used annotations
The @ SpringBootApplication annotation is equivalent to using @ Configuration, @ EnableAutoConfiguration, and @ ComponentScan with their default attributes
9. Development Tools
The spring-boot-devtools module can be added in any project to provide additional development-time features.
<Dependencies>
<Dependency>
<GroupId> org. springframework. boot </groupId>
<ArtifactId> spring-boot-devtools </artifactId>
<Optional> true </optional>
</Dependency>
</Dependencies>
It supports code modification and re-loading and startup. If you do not need:
If you do not want to use the restart feature, you can disable it by using the spring. devtools. restart. enabled property. in most cases, you can set this property in your application. properties (doing so still initializes the restart classloader, but it does not watch for file changes ).
If you need to completely disable restart support (for example, because it does not work with a specific library), you need to set the spring. devtools. restart. enabled System property to false before calling SpringApplication. run (...), As shown in the following example:
Public static void main (String [] args ){
System. setProperty ("spring. devtools. restart. enabled", "false ");
SpringApplication. run (MyApp. class, args );
}
If you do not want to start the application, you must set the specified file modification in application. properties to restart the application:
# Create a file restart. trigger in the resouces folder and set
Spring. devtools. restart. trigger-file = restart. trigger
In this way, the system restarts only when the file restart. trigger is changed.