在项目中配置继承spring-boot-starter-parent,能够进行以下设置:html
<!-- Inherit defaults from Spring Boot --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.BUILD-SNAPSHOT</version> </parent>
注意,设置版本号时,只须要在该parent中设置,若是想增长starters,则能够自动优化这些starters的版本信息。java
在如上配置中,若是想覆盖独立的依赖,能够经过在项目中覆盖默认属性便可,以下所示:react
<properties> <spring-data-releasetrain.version>Fowler-SR2</spring-data-releasetrain.version> </properties>
若是不使用spring-boot-starter-parent, 能够经过使用scope=import依赖,进而保留依赖管理的好处。以下所示:web
<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>
上述配置不容许使用property属性覆盖独立的依赖,若是想要实现覆盖,能够在项目中的dependencyManagement,做以下设置,且注意独立的依赖须要在spring-boot-dependencies以前。redis
<dependencyManagement> <dependencies> <!-- Override Spring Data release train provided by Spring Boot --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-releasetrain</artifactId> <version>Fowler-SR2</version> <scope>import</scope> <type>pom</type> </dependency> <dependency> <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>
Spring Boot包含Maven插件,可以将项目打包为可执行的jar包。能够在<plugins>中设置。以下所示:spring
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
一样说明的是,若是使用parent依赖,则该插件不须要进一步配置。mongodb
Spring Boot的Starters提供了Spring及其相关技术等方便的依赖描述,例如,若是须要使用Spring和JPA来访问数据库,能够在项目中增长spring-boot-starter-data-jpa依赖便可。目前支持的starters以下:数据库
名称 | 说明 |
spring-boot-starter | 核心启动器,包括自动配置支持,日志记录和YAML |
spring-boot-starter-activemq | 使用Apache ActiveMQ的JMS启动器 |
spring-boot-starter-amqp | 使用Spring AMQP和Rabbit MQ的启动器 |
spring-boot-starter-aop | 使用Spring AOP和AspectJ进行面向切面编程的启动器 |
spring-boot-starter-artemis | 使用Apache Artemis的JMS启动器 |
spring-boot-starter-batch | 使用Spring Batch的启动器 |
spring-boot-starter-cache | 使用Spring Framework缓存支持的启动器 |
spring-boot-starter-cloud-connectors | 使用Spring Cloud链接器,简化了与Cloud Foundry和Heroku等云平台中的服务链接的启动器 |
spring-boot-starter-data-cassandra | 使用Cassandra分布式数据库和Spring Data Cassandra的启动器 |
spring-boot-starter-data-cassandra-reactive | 使用Cassandra分布式数据库和Spring Data Cassandra Reactive的启动器 |
spring-boot-starter-data-couchbase | 使用Couchbase面向文档的数据库和Spring Data Couchbase的启动器 |
spring-boot-starter-data-couchbase-reactive | 使用Couchbase面向文档的数据库和Spring Data Couchbase Reactive的启动器 |
spring-boot-starter-data-elasticsearch | 使用Elasticsearch搜索和分析引擎和Spring Data Elasticsearch的启动器 |
spring-boot-starter-data-jpa | 使用Spring数据JPA与Hibernate的启动器 |
spring-boot-starter-data-ldap | 使用Spring Data LDAP的启动器 |
spring-boot-starter-mongodb | 使用MongoDB面向文档的数据库和Spring Data MongoDB的启动器 |
spring-boot-starter-mongodb-reactive | 使用MongoDB面向文档的数据库和Spring Data MongoDB Recative的启动器 |
spring-boot-starter-neo4j | 使用Neo4j图数据库和Spring Data Neo4j的启动器 |
spring-boot-starter-redis | Redis key-value 数据存储与Spring Data Redis和Jedis客户端启动器 |
spring-boot-starter-redis-reactive | Redis key-value 数据存储与Spring Data Redis Reactive和Jedis客户端启动器 |
spring-boot-starter-data-rest | 经过使用Spring Data REST在REST上暴露Spring数据库的启动器 |
spring-boot-starter-data-solr | 使用Apache Solr搜索平台与Spring Data Solr的启动器 |
spring-boot-starter-freemarker | 使用FreeMarker视图构建MVC Web应用程序的启动器 |
spring-boot-starter-groovy-templates | 使用Groovy模板视图构建MVC Web应用程序的启动器 |
spring-boot-starter-hateoas | 使用Spring MVC和Spring HATEOAS构建基于超媒体的RESTful Web应用程序的启动器 |
spring-boot-starter-integration | Spring Integration 启动器 |
spring-boot-starter-jdbc | 使用JDBC与Tomcat JDBC链接池的启动器 |
spring-boot-starter-jersey | 使用JAX-RS和Jersey构建RESTful Web应用程序的启动器。spring-boot-starter-web的替代方案 |
spring-boot-starter-jooq | 使用jOOQ访问SQL数据库的启动器。 spring-boot-starter-data-jpa或spring-boot-starter-jdbc的替代方案 |
spring-boot-starter-json | 用于读取和写入json的启动器 |
spring-boot-starter-jta-atomikos | 使用Atomikos的JTA事务的启动器 |
spring-boot-starter-jta-bitronix | 使用Bitronix进行JTA 事务的启动器 |
spring-boot-starter-jta-narayana | Spring Boot Narayana JTA 启动器 |
spring-boot-starter-mail | Java Mail和Spring Framework的电子邮件发送支持的启动器 |
spring-boot-starter-mustache | 使用Mustache视图构建MVC Web应用程序的启动器 |
spring-boot-starter-quartz | Spring Boot Quartz启动器 |
spring-boot-starter-security | 使用Spring Security的启动器 |
spring-boot-starter-test | 使用JUnit,Hamcrest和Mockito的库测试Spring Boot应用程序的启动器 |
spring-boot-starter-thymeleaf | 使用Thymeleaf视图构建MVC Web应用程序的启动器 |
spring-boot-starter-validation | 使用Java Bean Validation 与Hibernate Validator的启动器 |
spring-boot-starter-web | 使用Spring MVC构建Web,包括RESTful应用程序。使用Tomcat做为默认的嵌入式容器的启动器 |
spring-boot-starter-web-services | Spring Web Services 启动器 |
spring-boot-starter-webflux编程 |
Starter for building WebFlux applications using Spring Framework’s Reactive Web support |
spring-boot-starter-websocket | Starter for building WebSocket applications using Spring Framework’s WebSocket support |
spring-boot-starter-actuator | 使用Spring Boot Actuator提供生产准备功能,可帮助您监控和管理应用程序的启动器 |
spring-boot-starter-jetty | 使用Jetty做为嵌入式servlet容器的启动器。 spring-boot-starter-tomcat的替代方案 |
spring-boot-starter-log4j2 | 使用Log4j2进行日志记录的启动器。 spring-boot-start-logging的替代方法 |
spring-boot-starter-logging | 使用Logback进行日志记录的启动器。 默认的日志启动器 |
spring-boot-starter-reactor-netty | 使用Reactive Netty做为嵌入式reactive http服务器 |
spring-boot-starter-tomcat | 使用Tomcat做为嵌入式servlet容器的启动器。 spring-boot-starter-web的默认servlet容器启动器 |
spring-boot-starter-undertow | 使用Undertow做为嵌入式servlet容器的启动器。 spring-boot-starter-tomcat的替代方案 |
Spring Boot不须要任何任何特殊的代码组织,但有以下一些实践经验:json
类中不包含"package"声明,即便用默认包。默认包尽可能避免,在Spring Boot中使用@ComponentScan, @EntityScan或@SpringBootApplication注解时,会致使特殊异常。
一般建议将主应用类放在其余类之上根包中,@EnableAutoConfiguration注解常常会放在主类中。以下显示典型的层级:
com +- example +- myapplication +- Application.java | +- customer | +- Customer.java | +- CustomerController.java | +- CustomerService.java | +- CustomerRepository.java | +- order +- Order.java +- OrderController.java +- OrderService.java +- OrderRepository.java
Application.java中应该声明main方法,以及一些基础的@Configuration注解等,以下:
package com.example.myapplication; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration @EnableAutoConfiguration @ComponentScan public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
Spring Boot一般建议配置时,主source是@Configuration类,一般,定义main方法的类也是做为主要的@Configuration一个很好的选择。
不须要将全部的@Configuration放在一个类中,@Import注解用于导入其余额外的配置类。另外,也可以使用@CompomentScan注解自动选取全部的Spring组件,包含@Configutation类。
若是必须使用基于XML的配置,建议仍以@Configuration类开始,而后使用@ImportResource注解导入XML配置文件。
Spring Boot的自动配置会基于所添加的依赖包进行自动配置Spring应用。例如若HSQDB在classpath中,且没有手动配置任何数据库链接,Spring Boot会自动配置该内存数据库。
自动配置时,能够将@EnableAutoConfiguration或@SpringBootApplication增长到@Configuration类中。注意,应该只在主要的@Configuration类中增长一个@EnableAutoConfiguration注解。
自动配置是非侵入式的,你能够定义本身的配置来替代自动配置。若是想查看当前哪些自动配置在使用,可以使用-debug开关启动应用程序,这将启动debug日志,并将自动配置日志记录到控制台。
2) 禁用指定的自动配置
若是你不想使用一些指定的自动配置类,可使用@EnableAutoConfiguration的exclude属性,以下例所示:
import org.springframework.boot.autoconfigure.*; import org.springframework.boot.autoconfigure.jdbc.*; import org.springframework.context.annotation.*; @Configuration @EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class}) public class MyConfiguration { }
若是类不在classpath中,可使用excludeName属性指定全限定名。最后,也能够禁用自动配置类列表,经过使用spring.autoconfigure.exclude属性。
能够随意使用标准Spring框架技术定义本身的beans及注入依赖。发现能够经过使用@ComponentScan注解来查找beans,使用@Autowired构造函数注入效果很好。
若是按照上述建议(将应用类放在根包中)构建代码,则能够增长@ComponentScan,且不须要任何参数。全部的应用组件如@Compent, @Service, @Reposity, @Controller等都将自动注册为Spring Beans。
下例展现了一个@Service Bean,经过构造函数注入获取一个RiskAssessor bean。
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class DatabaseAccountService implements AccountService { private final RiskAssessor riskAssessor; @Autowired public DatabaseAccountService(RiskAssessor riskAssessor) { this.riskAssessor = riskAssessor; } }
若是一个bean只有一个构造函数,则能够省略@Autowired方法。注意如何使用构造方法注入使得riskAssessor标记为final, 后续使用该变量时则不可改变。
@Service public class DatabaseAccountService implements AccountService { private final RiskAssessor riskAssessor; public DatabaseAccountService(RiskAssessor riskAssessor) { this.riskAssessor = riskAssessor; } }
许多Spring Boot开发者常常在主类中加入@Configuration, @EnableAutoConfiguration, @CompomentScan等注解, Spring Boot提供一个更简洁的选择@SpringBootApplication。以下所示:
package com.example.myapplication; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
1) 运行jar包程序,可使用以下命令:
java -jar target/myapplication-0.0.1-SNAPSHOT.jar # 以远程调试的方式运行 java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=8000,suspend=n -jar target/myapplication-0.0.1-SNAPSHOT.jar
2) 使用Maven插件,直接运行 mvn spring-boot:run,若是想使用MAVEN_OPTS环境参数,可使用 export MAVEN_OPTS=-Xmx1024m
spring-boot-devtools模块可提供额外的开发时功能,能够经过以下方法加载该依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
缓存在生产中很是有益,但在开发过程当中可能产生反效果。spring-boot-devtools将默认禁用缓存选项。缓存选项一般在application.properties中设置,而spring-boot-devtools模块不须要手动设置这些属性,而是自动应用更加合理的development-time配置。
使用spring-boot-devtools的应用程序将在类路径上的文件发生更改时自动重启。且默认状况下,将监视指向文件夹的类路径上的任何条目。注意,静态资源和视图模板不须要从新启动应用程序。
Spring boot提供的从新启动技术使用两个类加载器。不会更改的类如第三方的jar被加载到基类加载器中。正在开发的类被加载到从新启动类加载器中。当应用程序从新启动时,从新启动类加载器将被丢弃,并建立一个新的类加载器。
在类路径下,某些资源更改时不必定须要触发从新启动。默认状况下,有一些排除项,如/META-INF/maven, /META-INF/resources, /resources/, /static, /public或/templates中的资源不会触发启动,但会重发实施从新加载。若是要自定义排除项,可使用spring.devtools.restart.exclude属性。如要仅排除/static和/public,能够设置:spring.devtools.restart.exclude=static/**,public/**。若是要保留这些默认值并添加其余排除项,可以使用spring-devtools.restart.additional-exclude属性。
有时须要监视不在类路径中的文件更改,进而从新启动或从新加载应用程序,可以使用spring.devtools.restart.additional-paths属性配置其余路径。可使用上述的spring.devtools.restart.exclude属性控制附加路径下的更改是否会触发彻底从新启动或只是实施从新加载。
若是不想使用从新启动功能,可使用spring.devtools.restart.enabled属性来禁用。如须要彻底禁用从新启动,在调用SpringApplication.run以前设置System属性。
public static void main(String[] args) { System.setProperty("spring.devtools.restart.enabled", "false"); SpringApplication.run(MyApp.class, args); }
如使用IDE编写代码或更改文件,你可能但愿仅在特定时间触发从新启动,可使用"触发文件"。可以使用spring.devtools.restart.trigger-file属性。
如上所述,IDE默认状况下打开的项目将使用"从新启动"类加载器加载,任何常规jar将使用基类加载器加载。若在多模块项目中工做,则可能须要自定义事件。为此,能够建立一个META-INF/spring-devtools.properties。spring-devtools.properties文件能够包含restart.exclude和restart.include.prefixed属性。如:
restart.exclude.companycommonlibs=/mycorp-common-[\\w-]+\.jar restart.include.projectcommon=/mycorp-myproj-[\\w-]+\.jar
spring-boot-devtools包含一个嵌入式LiveReload服务器,能够在资源更改时用于触发浏览器更新。若是不想在应用程序中启动LiveReload服务器,可将spring.devtools.livereload.enabled属性设为false。
能够向$HOME文件夹添加名为.spring-boot-devtools.properties的文件配置全局devtools设置(注意以"."开头)。添加到此文件的任何属性将适用于所在计算机上使用devtools的全部spring boot应用程序。例如:要配置从新启动以始终使用触发器文件,能够在~/.spring-boot-devtools.properties中添加:spring.devtools.reload.trigger-file=.reloadtrigger。
spring boot不只限于本地开发,也支持远程运行。远程支持是可选的,若是想使用该功能,首先须要确保devtools包含在从新打包存档中,以下所示:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludeDevtools>false</excludeDevtools>
</configuration>
</plugin>
</plugins>
</build>
而后须要设置spring.devtools.remote.secret属性,如:spring.devtools.remote.secret=mysecret。注意:生产环境禁用远程调用。
远程devtools支持包含两部分:接收链接的服务端和运行在IDE上的客户端程序。当spring.devtools.remote.secret设置时,服务端部分自动可用。客户端部分则必须手动启动。