spring boot 文档学习笔记day01

Posted 月光莫里亚

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring boot 文档学习笔记day01相关的知识,希望对你有一定的参考价值。

1.在pom.xml中使用spring-boot-starter-parent的作用:

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.xml里面有哪些配置,太多了,网址:

https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-dependencies/pom.xml

 

3.启动类的位置最好在最外层的包中:

We generally recommend that you locate your main application class in a root package above 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.尽量使用java配置,避免使用xml配置

You need not put all your @Configuration into a single class. The @Import annotation can be used to import additional configuration classes. Alternatively, you can use @ComponentScan to automatically pick up all Spring components, including @Configuration classes.

If you absolutely must use XML based configuration, we recommend that you still start with a @Configuration class. You can then use an @ImportResource annotation to load XML configuration files.

 

5.自动配置

You need to opt-in to auto-configuration by adding the @EnableAutoConfiguration or @SpringBootApplication annotations to one of your @Configuration classes.,只添加到一个即可

 

 

6.排除不需要自动配置的类

@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})

或者

you can also control the list of auto-configuration classes to exclude by using the spring.autoconfigure.exclude property.

 

7.Bean注入容器

If you structure your code as suggested above (locating your application class in a root package), you can add @ComponentScan without any arguments. All of your application components (@Component, @Service, @Repository, @Controller etc.) are automatically registered as Spring Beans.

 

8. @SpringBootApplication代替常用几个的注解

The @SpringBootApplication annotation is equivalent to using @Configuration, @EnableAutoConfiguration, and @ComponentScan with their default attributes

 

9.开发工具

The spring-boot-devtools module can be included 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>

支持代码修改后重新加载启动,如果不需要:

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);

}

在开过程中经常修改但是不想启动,application.properties里面设置指定的文件修改才重启:

#重启的文件resouces文件夹中新建文件restart.trigger并设置

spring.devtools.restart.trigger-file=restart.trigger

这样只有在restart.trigger这个文件更改时才重新启动

 


以上是关于spring boot 文档学习笔记day01的主要内容,如果未能解决你的问题,请参考以下文章

day01-SpringBoot基本介绍

Spring Boot 官方文档学习入门及使用[转]

Spring Boot 自学笔记01

滴滴架构师手码10W字Spring Boot高级进阶笔记,值得一看!

Spring Boot 官方文档入门及使用

Spring Boot文档阅读笔记-CORS Support