Spring Boot requires a separate container to run it?
May not be required, it built Tomcat / Jetty like container. By introducing the pom.xml dependency:
<- spring-boot-starter- web:! represents the web module, containing a number of JAR package in this module -> <- There are spring-related jar, built tomcat server, jackson, these web projects! common features are automatically incorporated -> <dependency> <the groupId> org.springframework.boot </ the groupId> <the artifactId> Starter-Spring-Boot-Web </ the artifactId> </ dependency>
Core comment Spring Boot Which? It mainly consists of several notes which composed?
Start class above annotations are @SpringBootApplication
, it is also the core Spring annotation Boot, mainly contains the following three combinations of notes:
@SpringBootConfiguration
@EnableAutoConfiguration
: Open function automatically configured, an option can also turn off auto-configuration, such as the data source close auto-configuration feature: @SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
.
@ComponentScan
: Spring component scans.
How to understand the meaning Starters in Spring Boot?
What Starters that?
Starters can be understood as a starter, which contains a series of applications which can be integrated into the dependencies, you can integrate Spring and other one-stop technology, without the need to find that sample code and dependencies. If you want to use the Spring JPA to access the database, just add spring-boot-starter-data-jpa
starter depend ready to use. Starters include the dependence of many projects need to use, they can continue to run fast, are supported by a series of management transitive dependencies.
Starters naming?
Spring Boot are based on the official starter spring-boot-starter-
named, it represents a particular type of application. Third-party launcher does not start with spring-boot name, they are the official Spring Boot reserved. On average, a third party should be so named, like the mybatis mybatis-spring-boot-starter
.
Starters Category:
1) provided by the initiator Spring boot
Initiator name | Functional Description |
---|---|
spring-boot-starter | The core module includes an automatic configuration support, log database and support for YAML configuration file. |
spring-boot-starter-amqp | Supports AMQP protocol (Advanced Message Queuing Protocol) via spring-rabbit |
spring-boot-starter-aop | I.e. oriented programming support AOP, comprising a spring-aop and AspectJ |
spring-boot-starter-artemis | JMS support through Apache Artemis API (Java Message Service API) |
spring-boot-starter-batch | Support Spring Batch, including HSQLDB database |
spring-boot-starter-cache | Spring's support for Cache abstract |
spring-boot-starter-cloud-connectors | Support Spring Cloud Connectors, simplified in such as Cloud Foundry or Heroku cloud platform connectivity services |
spring-boot-starter-data-elasticsearch | Support ElasticSearch search and analysis engine, including spring-data-elasticsearch |
spring-boot-starter-data-gemfire | GemFire supports distributed data storage, comprising a spring-data-gemfire |
spring-boot-starter-data-jpa | 支持 JPA (Java Persistence API), 包括 spring-data-jpa, spring-orm, Hibernate |
spring-boot-starter-data-solr | Support Apache Solr search platform, including spring-data-solr |
spring-boot-starter-data-mongodb | MongoDB support data, including spring-data-mongodb |
spring-boot-starter-data-rest | By spring-data-rest-webmvc, supported by data warehouse exposed Spring Data REST |
spring-boot-starter-redis | Redis key-value store database support, including spring-redis |
spring-boot-starter-data-jdbc | Supports JDBC database access |
spring-boot-starter-jta-atomikos | By Atomikos support JTA distributed transaction processing |
spring-boot-starter-jta-bitronix | By Bitronix support JTA distributed transaction processing |
spring-boot-starter-security | Support spring-security |
spring-boot-starter-test | Conventional tests rely support, including JUnit, Hamcrest, Mockito and spring-test module |
spring-boot-starter-velocity | Velocity template engine support |
spring-boot-starter-freemarker | Support FreeMarker template engine |
spring-boot-starter-thymeleaf | Support Thymeleaf template engine, including integration with Spring |
spring-boot-starter-mustache | Support Mustache template engine |
spring-boot-starter-web | Support full-stack Web development, including Tomcat and spring-webmvc |
spring-boot-starter-websocket | WebSocket support development |
spring-boot-starter-ws | Spring Web Services Support |
spring-boot-starter-groovy-templates | Groovy template engine support |
spring-boot-starter-hateoas | Supports HATEOAS of RESTful Web services via spring-hateoas |
spring-boot-starter-hornetq | By HornetQ JMS support |
spring-boot-starter-log4j | Support Log4J logging framework |
spring-boot-starter-logging | Introduces Spring Boot default logging framework Logback |
spring-boot-starter-integration | It supports common spring-integration module |
spring-boot-starter-jersey | Support services framework Jersey RESTful Web |
spring-boot-starter-mail | Support javax.mail module |
spring-boot-starter-mobile | Support spring-mobile |
spring-boot-starter-social-facebook | 支持 spring-social-facebook |
spring-boot-starter-social-linkedin | Support spring-social-linkedin |
spring-boot-starter-social-twitter | Support spring-social-twitter |
spring-boot-starter-actuator | Product line for increasing the related functions, such as measurement and monitoring |
spring-boot-starter-remote-shell | Added support of remote ssh shell |
spring-boot-starter-tomcat | 引入了 Spring Boot 默认的 HTTP 引擎 Tomcat |
spring-boot-starter-jetty | 引入了Jetty HTTP引擎(用于替换Tomcat) |
spring-boot-starter-undertow | 引入了Undertow HTTP引擎(用于替换Tomcat) |
4)其他第三方启动器(略)
Spring Boot实现热部署有哪几种方式?
在Spring Boot实现代码热部署是一件很简单的事情,代码的修改可以自动部署并重新热启动项目。
1)引用devtools依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency>
修改一个java类时就可以实现热更新了。
2)自定义配置热部署
以下配置用于自定义配置热部署,可以不设置。
#热部署开关,false即不启用热部署
spring.devtools.restart,enabled: true
#指定热部署的目录
#spring.devtools.restart.additional-paths: src/main/java
#指定目录不更新
spring.devtools.restart.exclude: test/**
3)Intellij Idea
工具修改实现热部署
需要改以下两个位置:
勾上自动编译或者手动重新编译
File > Settings > Compiler-Build Project automatically
注册使用快捷键的方式:
ctrl + shift + alt + / > Registry > 勾选Compiler autoMake allow when app running
注意事项:
1)生产环境devtools将被禁用,如java -jar
方式或者自定义的类加载器等都会识别为生产环境。
2)打包应用默认不会包含devtools,除非你禁用SpringBoot Maven
插件的excludeDevtools
属性。
3)Thymeleaf无需配置 spring.thymeleaf.cache:false
,devtools默认会自动设置,参考完整属性。
下面是devtools自动配置的完整源码:
@Order(Ordered.LOWEST_PRECEDENCE) public class DevToolsPropertyDefaultsPostProcessor implements EnvironmentPostProcessor { private static final Map<String, Object> PROPERTIES; static { Map<String, Object> devToolsProperties = new HashMap<>(); devToolsProperties.put("spring.thymeleaf.cache", "false"); devToolsProperties.put("spring.freemarker.cache", "false"); devToolsProperties.put("spring.groovy.template.cache", "false"); devToolsProperties.put("spring.mustache.cache", "false"); devToolsProperties.put("server.servlet.session.persistent", "true"); devToolsProperties.put("spring.h2.console.enabled", "true"); devToolsProperties.put("spring.resources.cache.period", "0"); devToolsProperties.put("spring.resources.chain.cache", "false"); devToolsProperties.put("spring.template.provider.cache", "false"); devToolsProperties.put("spring.mvc.log-resolved-exception", "true"); devToolsProperties.put("server.servlet.jsp.init-parameters.development", "true"); devToolsProperties.put("spring.reactor.stacktrace-mode.enabled", "true"); PROPERTIES = Collections.unmodifiableMap(devToolsProperties); } @Override public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { if (isLocalApplication(environment) && canAddProperties(environment)) { PropertySource<?> propertySource = new MapPropertySource("refresh", PROPERTIES); environment.getPropertySources().addLast(propertySource); } } private boolean isLocalApplication(ConfigurableEnvironment environment) { return environment.getPropertySources().get("remoteUrl") == null; } private boolean canAddProperties(Environment environment) { return isRestarterInitialized() || isRemoteRestartEnabled(environment); } private boolean isRestarterInitialized() { try { Restarter restarter = Restarter.getInstance(); return (restarter != null && restarter.getInitialUrls() != null); } catch (Exception ex) { return false; } } private boolean isRemoteRestartEnabled(Environment environment) { return environment.containsProperty("spring.devtools.remote.secret"); } }
4)devtools会在windows资源管理器占用java进程,在开发工具里面杀不掉,只能手动kill掉,不然重启会选成端口重复绑定报错。
Spring Boot如何定义多套不同环境配置?
简单来说,Profile就是Spring Boot可以对不同环境或者指令来读取不同的配置文件。
假如有开发、测试、生产三个不同的环境,需要定义三个不同环境下的配置。
1)基于properties文件类型可以另外建立3个环境下的配置文件:
applcation.properties application-dev.properties application-test.properties application-prod.properties
然后在applcation.properties
文件中指定当前的环境spring.profiles.active=test
,这时候读取的就是application-test.properties
文件。
2)基于yml文件类型,只需要一个applcation.yml文件即可,推荐此方式。关注微信公众号“Java精选”(w_z90110),回复关键字领取资料:如Mysql
,Hadoop
,Dubbo
,CAS
源码等等,免费领取视频教程、资料文档和项目源码。Java面试题持续更新中...
spring: profiles: active: prod --- spring: profiles: dev server: port: 8080 --- spring: profiles: test server: port: 8081 --- spring.profiles: prod spring.profiles.include: - proddb - prodmq server: port: 8082 --- spring: profiles: proddb db: name: mysql --- spring: profiles: prodmq mq: address: localhost
其中dev代表开发,test代表测试,prod代表正式环境。此时读取的就是prod的配置,prod包含proddb,prodmq,此时可以读取proddb,prodmq下的配置。也可以同时激活三个配置,如下:
spring.profiles.active: prod,proddb,prodmq
3)基于Java代码,在JAVA配置代码中也可以加不同Profile下定义不同的配置文件,@Profile
注解只能组合使用@Configuration
和@Component
注解。
@Configuration @Profile("prod") public class ProductionConfiguration { // ... }
4)指定Profile
main方法启动方式:在Eclipse Arguments
里面添加
--spring.profiles.active=prod
插件启动方式:
spring-boot:run -Drun.profiles=prod
jar运行方式:
java -jar xx.jar --spring.profiles.active=prod
除了在配置文件和命令行中指定Profile,还可以在启动类中写死指定,通过SpringApplication.setAdditionalProfiles
方法
SpringApplication.class public void setAdditionalProfiles(String... profiles) { this.additionalProfiles = new LinkedHashSet<String>(Arrays.asList(profiles)); }