本文为学习Spring Boot实战的学习笔记,学习了一遍,但是好记性不如烂笔头,所以文章记录下来。图书购买地址为: https://item.jd.com/11894632.html.
1.Spring 基础
2.Spring 常用配置
3.Spring 高级话题
4.Spring mvc基础
5.Spring Boot基础
5.1 Spring boot概述
5.1.1 什么是spring boot
使用习惯优于配置的理念.不用或者只需要很少的spring配置.
5.1.2 Spring boot核心功能
- 1.独立运行的Spring 项目
支持用jar包的形式运行. - 2.内嵌Servlet容器
内嵌Tomcat,jetty - 3.提供starter,简化maven配置.
- 4.自动配置Spring
- 5.准生产的应用监控
http,ssh,telnet对运行时的项目进行监控 - 6.无代码生产和XML配置
5.1.3 Spring boot优缺点
优点
- 1)快速构建项目
- 2)对主流开发框架的无配置集成
- 3)项目可以独立运行,无须外部依赖servlet容器
- 4)提供运行时的应用监控
- 5)机极大的提高了开发,部署效率
- 6)与云计算的天然集成.
缺点
- 1)书籍文档少
- 2)不认可spring,可能不习惯
5.1.4 关于本书Spring boot版本
Spring boot 1.3.0
5.2 Spring boot快速搭建
5.2.1 http://start.spring.io
进入该页面能协助创建spring boot项目.
5.2.2 Spring Tool Suite
工具不错.
5.2.3 Intellij IDEA
工具口碑好
5.2.4 Spring boot CLI
下载链接
查看10.2章节.
5.2.5 maven 手工构建
5.2.6 简单演示
@SpringBootApplication :开启自动配置.
通过maven命令: mvn spring-boot:run
6.Spring Boot核心
6.1 基本配置
6.1.1 入口类和@SpringBootApplicaion
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Configuration
@EnableAutoConfiguration
@ComponentScan
public @interface SpringBootApplication {
}
@EnableAutoConfiguration让Spring boot根据类路径中的jar包依赖为当前项目进行自动配置.
Spring Boot启动类要放在groupId+arctifactID目录下.
6.1.2 关闭特定的自动配置
@SpringBootApplication(exclude={
DateSourceAutoConfiguration.class})
6.1.3 定制Banner
1) 修改banner内容
/src/main/resources目录下创建一个banner.txt文件,然后将ASCII字符画复制进去,就能替换默认的banner了.
2)属性设置.
属性 | 说明 |
---|---|
${AnsiColor.BRIGHT_RED} | 设置控制台中输出内容的颜色 |
${application.version} | 用来获取MANIFEST.MF文件中的版本号 |
${spring-boot.version} | Spring Boot的版本号 |
application.formatted−version|格式化后的
{application.version}版本信息
3)关闭banner方法1
SpringApplication application = new SpringApplication(App.class);
/*
* Banner.Mode.OFF:关闭;
* Banner.Mode.CONSOLE:控制台输出,默认方式;
* Banner.Mode.LOG:日志输出方式;
*/
application.setBannerMode(Banner.Mode.OFF);
application.run(args);
4)关闭banner方法2
在application.proerpties进行banner的显示和关闭:
是否显示banner,可选值[true|false]
spring.main.show-banner=false
6.1.4 Spring Boot的配置文件
application.properties或application.yml
//举几个例子:
server.port=9090
server.context-path=/helloboot
server:
port: 9090
contextPath:/helloboot
spring.redis.cluster.nodes[0]=192.168.0.1:6379
spring.redis.cluster.nodes[1]=192.168.0.2:6379
spring:
redis:
cluster:
nodes:
- 192.168.0.1:6379
- 192.168.0.2:6379
Spring Boot application.properties或application.yml相关配置
application.properties 改成 application.yml
6.1.5 Start pom
Spring Boot application starters 下面的应用程序starters是Spring Boot在org.springframework.boot组下提供的
名称 | 描述 |
---|---|
spring-boot-starter | 核心Spring Boot starter,包括自动配置支持,日志和YAML |
spring-boot-starter-actuator | 生产准备的特性,用于帮你监控和管理应用 |
spring-boot-starter-amqp | 对”高级消息队列协议”的支持,通过spring-rabbit实现 |
spring-boot-starter-aop | 对面向切面编程的支持,包括spring-aop和AspectJ |
spring-boot-starter-batch | 对Spring Batch的支持,包括HSQLDB数据库 |
spring-boot-starter-cloud-connectors | 对Spring Cloud Connectors的支持,简化在云平台下(例如,Cloud Foundry 和Heroku)服务的连接 |
spring-boot-starter-data-elasticsearch | 对Elasticsearch搜索和分析引擎的支持,包括spring-data-elasticsearch |
spring-boot-starter-data-gemfire | 对GemFire分布式数据存储的支持,包括spring-data-gemfire |
spring-boot-starter-data-jpa | 对”Java持久化API”的支持,包括spring-data-jpa,spring-orm和Hibernate |
spring-boot-starter-data-mongodb | 对MongoDB NOSQL数据库的支持,包括spring-data-mongodb |
spring-boot-starter-data-rest | 对通过REST暴露Spring Data仓库的支持,通过spring-data-rest-webmvc实现 |
spring-boot-starter-data-solr | 对Apache Solr搜索平台的支持,包括spring-data-solr |
spring-boot-starter-freemarker | 对FreeMarker模板引擎的支持 |
spring-boot-starter-groovy-templates | 对Groovy模板引擎的支持 |
spring-boot-starter-hateoas | 对基于HATEOAS的RESTful服务的支持,通过spring-hateoas实现 |
spring-boot-starter-hornetq | 对”Java消息服务API”的支持,通过HornetQ实现 |
spring-boot-starter-integration | 对普通spring-integration模块的支持 |
spring-boot-starter-jdbc | 对JDBC数据库的支持 |
spring-boot-starter-jersey | 对Jersey RESTful Web服务框架的支持 |
spring-boot-starter-jta-atomikos | 对JTA分布式事务的支持,通过Atomikos实现 |
spring-boot-starter-jta-bitronix | 对JTA分布式事务的支持,通过Bitronix实现 |
spring-boot-starter-mail | 对javax.mail的支持 |
spring-boot-starter-mobile | 对spring-mobile的支持 |
spring-boot-starter-mustache | 对Mustache模板引擎的支持 |
spring-boot-starter-redis | 对REDIS键值数据存储的支持,包括spring-redis |
spring-boot-starter-security | 对spring-security的支持 |
spring-boot-starter-social-facebook | 对spring-social-facebook的支持 |
spring-boot-starter-social-linkedin | 对spring-social-linkedin的支持 |
spring-boot-starter-social-twitter | 对spring-social-twitter的支持 |
spring-boot-starter-test | 对常用测试依赖的支持,包括JUnit, Hamcrest和Mockito,还有spring-test模块 |
spring-boot-starter-thymeleaf | 对Thymeleaf模板引擎的支持,包括和Spring的集成 |
spring-boot-starter-velocity | 对Velocity模板引擎的支持 |
spring-boot-starter-web | 对全栈web开发的支持,包括Tomcat和spring-webmvc |
spring-boot-starter-websocket | 对WebSocket开发的支持 |
spring-boot-starter-ws | 对Spring Web服务的支持 |
Spring Boot生产准备的starters
名称 | 描述 |
---|---|
spring-boot-starter-actuator | 添加生产准备特性,比如指标和监控 |
spring-boot-starter-remote-shell | 添加远程ssh |
Spring Boot technical starters
可用于排除或交换具体技术方面的starters。
名称 | 描述 |
---|---|
spring-boot-starter-jetty | 导入Jetty HTTP引擎(作为Tomcat的替代) |
spring-boot-starter-log4j | 对Log4J日志系统的支持 |
spring-boot-starter-logging | 导入Spring Boot的默认日志系统(Logback) |
spring-boot-starter-tomcat | 导入Spring Boot的默认HTTP引擎(Tomcat) |
spring-boot-starter-undertow | 导入Undertow HTTP引擎(作为Tomcat的替代) |
6.1.6 使用XML
@ImportResource 应用XML
@ImportResource({“classpath:some-context.xml”,”classpath:another-context.xml”})
6.2 外部配置
6.2.1 命令行参数配置
java -jar xx.jar
java -jar xx.jar --server.port=9090
6.2.2 常规属性配置
配置文件自动绑定到vo,属性上.
afsReqURL=http://192.168.2.80:8888/msDprAF088?
@Service
public class AfsVerifyBean {
@Value("${afsReqURL}")
private String reqURL;
}
6.2.3 类型安全的配置(基于properties)
这种还是蛮实用的.
application.properties中包含:
author.name=wyf
author.age=32
package com.wisely.ch6_2_3.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "author") //1
public class AuthorSettings {
private String name;
private Long age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getAge() {
return age;
}
public void setAge(Long age) {
this.age = age;
}
}
6.3 日志配置
支持java util logging,log4j,log4j2,logback,默认logback.
logback详解
6.4 Profile配置
spring.profiles.active=dev
//tomcat中的配置方法
export JAVA_OPTS="-Djava.library.path=/usr/local/lib -server -Xms1024m -Xmx1024m -XX:MaxPermSize=256m -Djava.awt.headless=true -Dsun.net.client.defaultConnectTimeout=60000 -Dsun.net.client.d
efaultReadTimeout=60000 -Djmagick.systemclassloader=no -Dnetworkaddress.cache.ttl=300 -Dsun.net.inetaddr.ttl=300 -Dspring.profiles.active=sit-vc"
export JAVA_HOME JAVA_BIN PATH CLASSPATH JAVA_OPTS
$CATALINA_HOME/bin/startup.sh -config $CATALINA_BASE/conf/server.xml
实战
6.5 Spring Boot运行原理
spring-boot-autoconfigure-1.3.0.x.jar中包含所有所有的自动配置.
自动配置有如下:
启动时控制台会有显示开启的配置和关闭的日志,日志如下
“`
=========================
AUTO-CONFIGURATION REPORT
Positive matches:
AopAutoConfiguration matched
-@ConditionalOnClassclassesfound: org.springframework.context.annotation.EnableAspectJAutoProxy,org.aspectj.lang.annotation.Aspect,org.aspectj.lang.reflect.Advice (OnClassCondition)
- matched (OnPropertyCondition)
AopAutoConfiguration.JdkDynamicAutoProxyConfiguration matched
- matched (OnPropertyCondition)
AuditAutoConfiguration.AuditEventRepositoryConfiguration matched
- @ConditionalOnMissingBean (types: org.springframework.boot.actuate.audit.AuditEventRepository; SearchStrategy: all) found no beans (OnBeanCondition)
DataSourceAutoConfiguration matched
-@ConditionalOnClassclassesfound: javax.sql.DataSource,org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType (OnClassCondition)
DataSourceAutoConfiguration.DataSourceInitializerConfiguration matched
-@ConditionalOnMissingBean(types:org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer; SearchStrategy: all) found no beans (OnBeanCondition)
DataSourceAutoConfiguration.JdbcTemplateConfiguration matched
- existing auto database detected (DataSourceAutoConfiguration.DataSourceAvailableCondition)
DataSourceAutoConfiguration.JdbcTemplateConfiguration#jdbcTemplate matched
- @ConditionalOnMissingBean (types: org.springframework.jdbc.core.JdbcOperations; SearchStrategy: all) found no beans (OnBeanCondition)
DataSourceAutoConfiguration.JdbcTemplateConfiguration#namedParameterJdbcTemplate matched
- @ConditionalOnMissingBean (types: org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations; SearchStrategy: all) found no beans (OnBeanCondition)
DataSourceAutoConfiguration.NonEmbeddedConfiguration matched
- supported DataSource class found (DataSourceAutoConfiguration.NonEmbeddedDataSourceCondition)
- @ConditionalOnMissingBean (types: javax.sql.DataSource,javax.sql.XADataSource; SearchStrategy: all) found no beans (OnBeanCondition)
DataSourcePoolMetadataProvidersConfiguration.TomcatDataSourcePoolMetadataProviderConfiguration matched
- @ConditionalOnClass classes found: org.apache.tomcat.jdbc.pool.DataSource (OnClassCondition)
DataSourceTransactionManagerAutoConfiguration matched
-@ConditionalOnClassclassesfound: org.springframework.jdbc.core.JdbcTemplate,org.springframework.transaction.PlatformTransactionManager (OnClassCondition)
DataSourceTransactionManagerAutoConfiguration#transactionManager matched
- @ConditionalOnBean (types: javax.sql.DataSource; SearchStrategy: all) found the following [dataSource] @ConditionalOnMissingBean (types: org.springframework.transaction.PlatformTransactionManager; SearchStrategy: all) found no beans (OnBeanCondition)
DataSourceTransactionManagerAutoConfiguration.TransactionManagementConfiguration matched
-@ConditionalOnMissingBean(types:org.springframework.transaction.annotation.AbstractTransactionManagementConfiguration; SearchStrategy: all) found no beans (OnBeanCondition)
DispatcherServletAutoConfiguration matched
- @ConditionalOnClass classes found: org.springframework.web.servlet.DispatcherServlet (OnClassCondition)
- found web application StandardServletEnvironment (OnWebApplicationCondition)
DispatcherServletAutoConfiguration.DispatcherServletConfiguration matched
- @ConditionalOnClass classes found: javax.servlet.ServletRegistration (OnClassCondition)
- no ServletRegistrationBean found (DispatcherServletAutoConfiguration.DefaultDispatcherServletCondition)
EmbeddedServletContainerAutoConfiguration matched
- found web application StandardServletEnvironment (OnWebApplicationCondition)
EmbeddedServletContainerAutoConfiguration.EmbeddedTomcat matched
- @ConditionalOnClass classes found: javax.servlet.Servlet,org.apache.catalina.startup.Tomcat (OnClassCondition)
-@ConditionalOnMissingBean(types:org.springframework.boot.context.embedded.EmbeddedServletContainerFactory; SearchStrategy: current) found no beans (OnBeanCondition)
EndpointAutoConfiguration#autoConfigurationReportEndpoint matched
-@ConditionalOnBean(types:org.springframework.boot.autoconfigure.condition.ConditionEvaluationReport; SearchStrategy: all) found the following [autoConfigurationReport] @ConditionalOnMissingBean (types: org.springframework.boot.actuate.endpoint.AutoConfigurationReportEndpoint; SearchStrategy: current) found no beans (OnBeanCondition)
EndpointAutoConfiguration#beansEndpoint matched
-@ConditionalOnMissingBean (types: org.springframework.boot.actuate.endpoint.BeansEndpoint; SearchStrategy: all) found no beans (OnBeanCondition)
EndpointAutoConfiguration#configurationPropertiesReportEndpoint matched
-@ConditionalOnMissingBean(types:org.springframework.boot.actuate.endpoint.ConfigurationPropertiesReportEndpoint; SearchStrategy: all) found no beans (OnBeanCondition)
EndpointAutoConfiguration#dumpEndpoint matched
-@ConditionalOnMissingBean (types: org.springframework.boot.actuate.endpoint.DumpEndpoint; SearchStrategy: all) found no beans (OnBeanCondition)
EndpointAutoConfiguration#environmentEndpoint matched
-@ConditionalOnMissingBean(types:org.springframework.boot.actuate.endpoint.EnvironmentEndpoint; SearchStrategy: all) found no beans (OnBeanCondition)
EndpointAutoConfiguration#healthEndpoint matched
-@ConditionalOnMissingBean (types: org.springframework.boot.actuate.endpoint.HealthEndpoint; SearchStrategy: all) found no beans (OnBeanCondition)
EndpointAutoConfiguration#infoEndpoint matched
- @ConditionalOnMissingBean (types: org.springframework.boot.actuate.endpoint.InfoEndpoint; SearchStrategy: all) found no beans (OnBeanCondition)
EndpointAutoConfiguration#metricsEndpoint matched
- @ConditionalOnMissingBean (types: org.springframework.boot.actuate.endpoint.MetricsEndpoint; SearchStrategy: all) found no beans (OnBeanCondition)
EndpointAutoConfiguration#shutdownEndpoint matched
-@ConditionalOnMissingBean(types: org.springframework.boot.actuate.endpoint.ShutdownEndpoint; SearchStrategy: all) found no beans (OnBeanCondition)
EndpointAutoConfiguration#traceEndpoint matched
- @ConditionalOnMissingBean (types: org.springframework.boot.actuate.endpoint.TraceEndpoint; SearchStrategy: all) found no beans (OnBeanCondition)
EndpointAutoConfiguration.RequestMappingEndpointConfiguration matched
- @ConditionalOnClass classes found: org.springframework.web.servlet.handler.AbstractHandlerMethodMapping (OnClassCondition)
EndpointAutoConfiguration.RequestMappingEndpointConfiguration#requestMappingEndpoint matched
- @ConditionalOnMissingBean (types: org.springframework.boot.actuate.endpoint.RequestMappingEndpoint; SearchStrategy: all) found no beans (OnBeanCondition)
EndpointMBeanExportAutoConfiguration matched
- JMX Endpoints (EndpointMBeanExportAutoConfiguration.JmxEnabledCondition)
EndpointWebMvcAutoConfiguration matched
- @ConditionalOnClass classes found: javax.servlet.Servlet,org.springframework.web.servlet.Dispatch