springboot学习

本文介绍SpringBoot框架的基本概念,包括其简化配置的特点、核心组成部分、常用配置参数等,适合初学者快速掌握。
springboot概念:
Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。这是百科上的讲解!主要的还是简化了各个插件,框架的配置,把主要的默认的配置信息都已经配置好啦,只需要配置一些核心的就可以正常使用啦!方便快速开发!适合于中小企业项目 


特点:
1. 创建独立的Spring应用程序
2. 嵌入的Tomcat,无需部署WAR文件
3. 简化Maven配置
4. 自动配置Spring
5. 提供生产就绪型功能,如指标,健康检查和外部配置
6. 绝对没有代码生成和对XML没有要求配置 [1] 


构成部分:启动类:Application.java(包括启动注解) + 核心配置文件application.properties/application.yml+jar包依赖pom.xml
主要的3构造




1.我们说下启动类里的相关启动注解:


入口类中需要用到的常用注解:


@SpringBootApplication:这是一个组合注解,聚合了多个注解的功能,建议进去看下源码(从源代码中得知 @SpringBootApplication 被 @Configuration、@EnableAutoConfiguration、@ComponentScan 注解所修饰)!包含:自启动项、自启动项的beanname、扫描包和扫描类。


@EnableAutoConfiguration:这个注解是用来启动springboot中的自动配置项目,必须的加上,否则无法正常使用springboot默认配置的配置项目(已被@SpringBootApplication注解包含)。


@ComponentScan :扫描controller层和service层中所有类上的注解(这个注解可以不加,因为组合注解@SpringBootApplication已经可以扫描)。


@MapperScan("com.zxz.mapper") :管理mybatis中所有mapper接口的代理对象。


@EnableTransactionManagement(proxyTargetClass = true):开启事务管理的注解。


2.application.properties配置文件


spring.config.name=自定义的配置文件名称


spring.config.location=配置文件位置(可以是classpath或者有效url)


也可以通过在自定义类上设置@PropertySource注解指定读取某个配置文件
下面简要说一部分spring-boot项目中application.properties内的一些常用配置,更多参照官方文档。


1. 命令行参数
server.address=xxx.xxx.xx.xxx  //服务器绑定ip地址,多网卡时可以指定


server.port=xxx 


//可以指定springboot内嵌容器启动的端口,默认使用tomcat容器时在8080端口,右键run- java application/springboot..,可以支持不同的容器,在引入不同的依赖时。当server.port=0时,表示自动扫面获取一个可用的端口。


ssl的安全访问配置




server.port=8443
server.ssl.key-store=classpath:keystore.jks
server.ssl.key-store-password=secret
server.ssl.key-password=another-secret
目前spring-boot不支持http和https同时启用的情况,只支持使用其中一个,如果需要同时使用,可以使用其他形式的实现方式。


该部分对应org.springframework.boot.autoconfigure.webServerProperties类。


#spring.profiles.active=dev时,则说明是指定使用application-dev.properties文件进行配置
#spring.profiles.active=pro
#tomcat 端口号
#server.port=8089
#server.maxHttpHeaderSize=9000
#输出日志文件,默认不输出   
logging.file=D:/run.log
#修改日志级别,默认为INFO  
logging.level.root=DEBUG
#打印sql语句
logging.level.org.springframework=WARN
logging.level.spring_boot.smapper=DEBUG


#数据库连接块
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/azj
spring.datasource.username=root
spring.datasource.password=ai425193
spring.datasource.driverClassName = com.mysql.jdbc.Driver


#默认为stat,即开启sql监控。这里加了个wall,表示同时开启sql防火墙  
spring.datasource.druid.filters=stat,wall  
#spring监控,hello.controller是我的控制层包名,也可以是服务层,用逗号分隔多个监控内容  
spring.datasource.druid.aop-patterns=spring_boot.controller.*  
#监控页面登录用户名  
spring.datasource.druid.StatViewServlet.loginUsername=admin
#监控页面登录密码  
spring.datasource.druid.StatViewServlet.loginPassword=123456


#mybatis配置
mybatis.mapper-locaitons=classpath:mybatis/*.xml
#mybatis.config-location: classpath:mybatis/mybatis-config.xml




此外还有一些不是很常用的如:


server.http2.enable=true/false


//该属性可以支持http2的协议类型,目前只支持tomcat和undertow的容器并且需要JDK1.8+.






官文上对于内嵌tomcat的配置参数也有很多,还可以编码来实现很多tomcat中的功能,不多说了,内容多,需要的看一下官网。






2. 系统属性/变量
开发/测试/生产环境配置
spring.profiles.active=xxxx


//该系统变量可以指明要使用的配置文件,一般应用于多环境配置分离,如生产环境(production),开发环境(development),测试环境(test)等,可以自定义,如开发环境配置文件为application-dev.properties,则spring.profiles.active=dev,在启动时会加载application-dev.properties配置文件。






关于Banner
项目启动时会打印一大坨符号组成的东西,看了好久感觉好像是打印的Spring这几个字母。


SpringBoot里叫banner配置。


banner.location=xxx.txt //可以自定义输出信息的位置


banner.charset=utf-8 //指定编码格式


spring.main.banner-mode=console/off    //banner图开启或者打印模式






Mysql数据源配置(引入spring-boot-starter-jdbc自动集成)
注解使用jdbcTemplate或者集成Mybatis(引入maven:mybatis-spring-boot-starter )


spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/company?allowMultiQueries=true
spring.datasource.username=root
spring.datasource.password=xxxx


该部分配置为spring数据源的配置,使用jdbcTemplate进行数据库操作(使用jdbcTempate,会自动提示导入spring-boot-starter-jdbc等依赖)。在使用单元测试时,同时运行多个可能会出现数据源冲突,此时可以通过下面的属性生成唯一的数据源名称


spring.datasource.generate-unique-name=true


spring.datasource.*  的属性集配置对应org.springframework.boot.autoconfigure.jdbcDataSourceProperties类。






MongoDB数据源配置(引入spring-boot-starter-data-mongodb自动集成)
spring.data.mongodb.*对应的属性类为MongoProperties.java


常用基本属性:


spring.data.mongodb.host=xxx //默认localhost


spring.data.mongodb.port=xxx //默认27017


spring.data.mongodb.database=xx //数据库


spring.data.mongodb.username=xx //数据库用户名


spring.data.mongodb.password=xx //数据库用户密码


上述参数还可以用一个代替:(上述参数不可与该参数同时使用)


spring.data.mongodb.uri=mongodb://localhost:27017/test


如果有密码:


mongodb://username:password@localhost:27017/dbname






Redis配置(引入spring-boot-starter-data-redis自动集成)
#spring.redis.url=
#上面url形式等同下面
#host和port默认值取值localhost,6379
spring.redis.host=localhost
spring.redis.port=6379


#链接池配置,下面是默认值


spring.pool.max-idle=8


spring.pool.min-idel=0


spring.pool.max-wait=-1 //-1禁用该属性,等待单位milliseconds


spring.pool.max-active=8


#密码,无密码时不需要
#spring.redis.password=
#spring.redis.ssl=true/false


对应类org.springframework.boot.autoconfigure.data.redis.RedisProperties.java类






MyBatis配置(引入mybatis-spring-boot-starter自动集成)
如果不适用xml映射文件配置,下面的不需要,直接使用注解可以实现


#本例子中放在resources/mybatis/mybatis-config.xml中


mybatis.config-location=classpath:mybatis/mybatis-config.xml


mybatis.mapper-locaitons=classpath:mybatis/mappings/*.xml






#下面的配置可以在mybatis-config.xml中配置


#mybatis.configuration.*=xxx
#别名实体包,多个逗号隔开
#mybatis.type-aliases-package=com.tom.bean
#类型转换器包,多个逗号隔开
#mybatis.type-handlers-package=com.tom.mybatis.handlers
#执行类型
#mybatis.executor-type=SIMPLE/REUSE/BATCH






cache配置(引入spring-boot-starter-cache自动集成)
对于缓存自动集成,spring-boot也提供了很多方案,这里说一下默认的spring-cache、ehcache、redis-cache的相关属性配置


spring.cache.type=cache/ehcache/redis....(参照CacheType类) //用于指明要使用的缓存类型


ehcache专用:


spring.cache.ehcache.config=classpath:cache/ehcache.xml //用于配置使用ehcache时配置文件所在位置






在使用默认的cache时(默认cache是采用concurrentMap实现的缓存,前提是没有引入spring-boot-starter-data-redis等其他缓存依赖)或者使用redis时,可以使用下面参数配置


spring.cache.cache-names=xxx,xxx,xxx   //配置缓存名称






redis专用:


spring.cache.redis.time-to-live=600000    //缓存有效时间,单位毫秒






spring.cache.*对应类为:org.springframework.boot.autoconfigure.cache.cacheProperties.java


完成上面的设置就可以在项目中使用spring注解,但是要使用@EnableCaching启用注解,具体cache使用详见下面的章节。






文件上传配置
#默认true


#spring.http.mutipart.enabled=true


#上传中转文件位置,
#spring.http.multipart.location=
#最大上传文件大小,默认1MB
spring.http.multipart.max-file-size=5MB
#最大请求大小,默认10MB
spring.http.multipart.max-request-size=20MB


对应类:org.springframework.boot.autoconfigure.web.MultipartProperties




3.pom.xml配置文件:
主要是相关依赖包的加入 及几个核心依赖包
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.demo</groupId>
<artifactId>spring_boot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring_boot</name>
<description>spring_boot</description>


<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>


<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>


<dependencies>
<!-- web包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 页面跳转thymeleaf依赖包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--mysql依赖包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 引入mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-joda</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.module</groupId>
            <artifactId>jackson-module-parameter-names</artifactId>
        </dependency>
<!-- mybatis分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!-- sql语句监控器 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>


Spring Boot 也提供了其它的启动器项目包括,包括用于开发特定类型应用程序的典型依赖项。


spring-boot-starter-web-services - SOAP Web Services


spring-boot-starter-web - Web 和 RESTful 应用程序


spring-boot-starter-test - 单元测试和集成测试


spring-boot-starter-jdbc - 传统的 JDBC


spring-boot-starter-hateoas - 为服务添加 HATEOAS 功能


spring-boot-starter-security - 使用 SpringSecurity 进行身份验证和授权


spring-boot-starter-data-jpa - 带有 Hibeernate 的 Spring Data JPA


spring-boot-starter-data-rest - 使用 Spring Data REST 公布简单的 REST 服务
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

rainjm

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值