spring boot

来源:互联网 发布:mac射手影音字幕位置 编辑:程序博客网 时间:2024/04/29 19:48
整理自 Spring Boot  2.0版本的官方Reference Guide的笔记
demo地址:https://git.oschina.net/w_j_x/springBootdemo

1、需要相关的plugin才可以打包
To create an executable jar we need to add the spring-boot-maven-plugin to our pom.xml
<plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>  <!-- 加上才会包含需要的依赖包 -->
                        </goals>
                    </execution>
                </executions>
            </plugin>

2、如果不想用parent pom依赖的话,可以写成
<dependencyManagement><dependencies><dependency><!-- Import dependency management from Spring Boot --><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>2.0.0.BUILD-SNAPSHOT</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement>

3、建议main class在所有包的顶层,这样会主动去扫下面的包

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 will be used to search for @Entity items.

Using a root package also allows the @ComponentScan annotation to 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.

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.) will be automatically registered as Spring Beans.


4、隐式的一些参数之类的

  • Java 1.8 as the default compiler level.
  • UTF-8 source encoding.
  • A Dependency Management section, allowing you to omit <version> tags for common dependencies, inherited from the spring-boot-dependencies POM.      <version>可省略,继承自spring-boot-dependencies POM
  • Sensible resource filtering.
  • Sensible plugin configuration (exec plugin, surefire, Git commit ID, shade).
  • Sensible resource filtering for application.properties and application.yml including profile-specific files (e.g. application-foo.properties andapplication-foo.yml)   自动去使用配置文件下的参数,例如server.port=9090 可指定端口为9090

On the last point: since the default config files accept Spring style placeholders (${…​}) the Maven filtering is changed to use @..@ placeholders (you can override that with a Maven property resource.delimiter).  


5、可使用@Configuration 注解导入基于XML的配置

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


6、启动时可以使用 --debug参数启动,可以查看debug级别日志,方便调试(例如在eclipse的启动参数里加入--debug即可)

If you need to find out what auto-configuration is currently being applied, and why, start your application with the --debug switch. This will enable debug logs for a selection of core loggers and log an auto-configuration report to the console.


7、过滤一些不想注入的类

If you find that specific auto-configure classes are being applied that you don’t want, you can use the exclude attribute of @EnableAutoConfiguration to disable them.

import org.springframework.boot.autoconfigure.*;import org.springframework.boot.autoconfigure.jdbc.*;import org.springframework.context.annotation.*;@Configuration@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})publicclass MyConfiguration {}

If the class is not on the classpath, you can use the excludeName attribute of the annotation and specify the fully qualified name instead. Finally, you can also control the list of auto-configuration classes to exclude via the spring.autoconfigure.exclude property.


8 、@SpringBootApplication 等于@Configuration, @EnableAutoConfiguration 和@ComponentScan三者之和
The @SpringBootApplication annotation is equivalent to using @Configuration, @EnableAutoConfiguration and @ComponentScan with their default attributes


9、热部署自动重启依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><optional>true</optional><!-- optional=true,依赖不会传递-->
</dependency>
10、缓存优化

默认不开启,可以在配置文件配置,配置项可参考:https://github.com/spring-projects/spring-boot/blob/master/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/env/DevToolsPropertyDefaultsPostProcessor.java

Cache options are usually configured by settings in your application.properties file. For example, Thymeleaf offers the spring.thymeleaf.cache property. Rather than needing to set these properties manually, the spring-boot-devtools module will automatically apply sensible development-time configuration.


11、自定义当某些资源改变时不要重启

Certain resources don’t necessarily need to trigger a restart when they are changed. For example, Thymeleaf templates can just be edited in-place. By default changing resources in /META-INF/maven/META-INF/resources ,/resources ,/static ,/public or /templates will not trigger a restart but will trigger a live reload. If you want to customize these exclusions you can use the spring.devtools.restart.exclude property. For example, to exclude only /static and /public you would set the following:

spring.devtools.restart.exclude=static/**,public/**
if you want to keep those defaults and add additional exclusions, use the spring.devtools.restart.additional-exclude property instead.
12、监视某些不在classpath下资源的变化时重启服务器
You may want your application to be restarted or reloaded when you make changes to files that are not on the classpath. To do so, use thespring.devtools.restart.additional-paths property to configure additional paths to watch for changes. You can use the spring.devtools.restart.excludeproperty described above to control whether changes beneath the additional paths will trigger a full restart or just a live reload.
13、设置不要自动重启

If you don’t want to use the restart feature you can disable it using the spring.devtools.restart.enabled property. In most cases you can set this in yourapplication.properties (this will still initialize the restart classloader but it won’t watch for file changes).

If you need to completely disable restart support, for example, because it doesn’t work with a specific library, you need to set a System property before callingSpringApplication.run(…​). For example:

publicstaticvoid main(String[] args) {    System.setProperty("spring.devtools.restart.enabled", "false");    SpringApplication.run(MyApp.class, args);}
待续。。。。。。。
0 0
原创粉丝点击