<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>
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 thespring-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
andapplication.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 @ImportResource
annotation 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.
@Configuration
, @EnableAutoConfiguration
和@ComponentScan三者之和
@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.
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.exclude
property 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); }待续。。。。。。。