一、修改默认配置

例1、spring boot 开发web应用的时候,默认tomcat的启动端口为8080,如果需要修改默认的端口,则需要在application.properties 添加以下记录:

server.port=8888

重启项目,启动日志可以看到:Tomcat started on port(s): 8888 (http) 启动端口为8888,浏览器中访问 http://localhost:8888 能正常访问。

例2、spring boot 开发中的数据库连接信息配置(这里使用com.alibaba 的 druid), 在application.properties 添加以下记录:

druid.url=jdbc:mysql://localhost:3306/cimc
druid.driver-class=com.mysql.jdbc.Driver
druid.username=root
druid.password=root
druid.initial-size=1
druid.min-idle=1
druid.max-active=20
druid.test-on-borrow=true

说明了如需修改starter模块中的默认配置,只需要在在application.properties 添加需要修改的配置即

附: application.properties 全部配置项,点击查看Spring Boot 所有配置说明

二、自定义属性配置

在application.properties中除了可以修改默认配置,我们还可以在这配置自定义的属性,并在实体bean中加载出来。

1、在application.properties中添加自定义属性配置

user=loaderman
userPassword=123456

2、编写Bean类,加载属性

User类需要添加@Component注解,让spring在启动的时候扫描到该类,并添加到spring容器中。
package com.generalichina.cimc.bean;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; @Component
public class User {
//获取application.properties的属性
@Value("${user}")
private String userName; @Value("${userPassword}")
private String userPassword; public String getUserName() {
return userName;
} public void setUserName(String userName) {
this.userName = userName;
} public String getUserPassword() {
return userPassword;
} public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
}

3、在controller中注入并使用User这个Bean。

package com.generalichina.cimc.controller;

import com.generalichina.cimc.bean.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class IndexController {
@Autowired
private User user;
@RequestMapping("/index")
public String index() {
System.out.println(user.getUserName() + " " + user.getUserPassword());
return "index";
}
}
浏览器访问:http://localhost:8888/index

控制台输出:loaderman 123456

三、application.properties 属性配置详解

1、参数引用与random随机数方法的使用

在application.properties内可以直接通过${}引用其他属性的值,如下:
user=loaderman
userPassword=123456${user}

输出: loaderman 123456loaderman

在application.properties中如果需要获取随机数,可以通过${random},如下:
#获取随机字符串
com.sam.randomValue=${random.value} #获取随机字符串:${random.value}
#获取随机int:${random.int}
#获取10以内的随机数:${random.int(10)}
#获取10-20的随机数:${random.int[10,20]}
#获取随机long:${random.long}
#获取随机uuid:${random.uuid}

2、多环境配置

实际开发中可能会有不同的环境,有开发环境、测试环境、生成环境。对于每个环境相关配置都可能有所不同,如:数据库信息、端口配置、本地路径配置等。

如果每次切换不同环境都需要修改application.properties,那么操作是十分繁琐的。在spring boot中提供了多环境配置,使得我们切换环境变得简便。

在application.properties同目录下新建一下三个文件:

application-dev.properties      //开发环境的配置文件
application-test.properties //测试环境的配置文件
application-prod.properties //生产环境的配置文件
上面三个文件分别对应了 开发、测试、生产 的配置内容,接下来就是应该怎么选择性引用这些配置了。

在application.properties添加:

spring.profiles.active=dev
#引用测试的配置文件
#spring.profiles.active=test
#引用生产的配置文件
#spring.profiles.active=prod
添加spring.profiles.active=dev后启动应用,会发现引用了dev的这份配置信息。
可以看出上面三个配置文件符合 application-{profile}.properties 格式,而在application.properties添加的 spring.profiles.active=dev 中的dev正是上面配置文件中的 profile。根据具体环境进行切换即刻。

补充:

在spring boot 中配置除了支持 application.properties,还支持application.yml的配置方式,如下:
新建application.yml代替application.properties
server:
port: 8080
spring:
datasource:
#name: druid
#type: com.alibaba.druid.pool.DruidDataSource
#druid相关配置
druid:
#监控统计拦截的filters
filters: stat
driver-class-name: oracle.jdbc.OracleDriver
#基本属性
url: jdbc:oracle:thin:@192.168.192.192:1521:ORCL
username: root
password: root
#配置初始化大小/最小/最大
initial-size: 5
min-idle: 5
max-active: 20
#获取连接等待超时时间
max-wait: 60000
#间隔多久进行一次检测,检测需要关闭的空闲连接
time-between-eviction-runs-millis: 60000
#一个连接在池中最小生存的时间
min-evictable-idle-time-millis: 300000
validation-query: select * from xxx
test-while-idle: true
test-on-borrow: false
test-on-return: false
#打开PSCache,并指定每个连接上PSCache的大小。oracle设为true,mysql设为false。分库分表较多推荐设置为false
pool-prepared-statements: true
max-pool-prepared-statement-per-connection-size: 20
mvc:
view:
prefix: /WEB-INF/jsp/
suffix: .jsp
resources:
add-mappings: false
jackson:
serialization:
write-dates-as-timestamps: true ## 该配置节点为独立的节点,有很多同学容易将这个配置放在spring的节点下,导致配置无法被识别
mybatis:
mapper-locations: classpath:mapper/*.xml #注意:一定要对应mapper映射xml文件的所在路径
type-aliases-package: com.sharewin.fcapp.pojo # 注意:对应实体类的路径
configuration:
use-column-label: true
use-generated-keys: true
map-underscore-to-camel-case: true logging:
config: classpath:logback-spring.xml
path: /var/log debug: true
注意:port: 8080中间是有空格的,yml语法请参考:yml配置文件用法

Spring Boot属性配置&自定义属性配置的更多相关文章

  1. Spring Boot 系列(三)属性配置&自定义属性配置

    在使用spring boot过程中,可以发现项目中只需要极少的配置就能完成相应的功能,这归功于spring boot中的模块化配置,在pom.xml中依赖的每个Starter都有默认配置,而这些默认配 ...

  2. Spring Boot 属性配置&自定义属性配置

    在使用spring boot过程中,可以发现项目中只需要极少的配置就能完成相应的功能,这归功于spring boot中的模块化配置,在pom.xml中依赖的每个Starter都有默认配置,而这些默认配 ...

  3. 51. spring boot属性文件之多环境配置【从零开始学Spring Boot】

    原本这个章节是要介绍<log4j多环境不同日志级别的控制的>但是没有这篇文章做基础的话,学习起来还是有点难度的,所以我们先一起了解下spring boot属性文件之多环境配置,当然文章中也 ...

  4. Spring Boot 属性配置和使用

    Spring Boot 属性配置和使用 Spring Boot 允许通过外部配置让你在不同的环境使用同一应用程序的代码,简单说就是可以通过配置文件来注入属性或者修改默认的配置. Spring Boot ...

  5. Spring Boot 属性配置和使用(转)

    Spring Boot 属性配置和使用 Spring Boot 允许通过外部配置让你在不同的环境使用同一应用程序的代码,简单说就是可以通过配置文件来注入属性或者修改默认的配置. Spring Boot ...

  6. Spring Boot实践——用外部配置填充Bean属性的几种方法

    引用:https://blog.csdn.net/qq_17586821/article/details/79802320 spring boot允许我们把配置信息外部化.由此,我们就可以在不同的环境 ...

  7. Spring boot 官网学习笔记 - Spring Boot 属性配置和使用(转)-application.properties

    Spring Boot uses a very particular PropertySource order that is designed to allow sensible overridin ...

  8. Spring Boot之实现自动配置

    GITHUB地址:https://github.com/zhangboqing/springboot-learning 一.Spring Boot自动配置原理 自动配置功能是由@SpringBootA ...

  9. 学记:为spring boot写一个自动配置

    spring boot遵循"约定优于配置"的原则,使用annotation对一些常规的配置项做默认配置,减少或不使用xml配置,让你的项目快速运行起来.spring boot的神奇 ...

  10. 【spring boot】spring boot中使用定时任务配置

    spring boot中使用定时任务配置 =============================================================================== ...

随机推荐

  1. WebApi增加Oauth2认证

    前期搭建可看这篇博文:https://www.cnblogs.com/lnice/p/6857203.html,此博文是在本篇博文实践才产生的,在实践中,也产生了几个问题,希望能够共同交流,一起进步. ...

  2. ElasticSearch 连载一 基础入门

    ElasticSearch简写ES,ES是一个高扩展.开源的全文检索和分析引擎,它可以准实时地快速存储.搜索.分析海量的数据. 应用场景 我们常见的商城商品的搜索 日志分析系统(ELK) 基于大量数据 ...

  3. redis 与 序列化

    概念 序列化:把对象转化为可传输的字节序列过程称为序列化. 反序列化:把字节序列还原为对象的过程称为反序列化. 为什么需要序列化 序列化最终的目的是为了对象可以跨平台存储,和进行网络传输.而我们进行跨 ...

  4. 关于WAMP的apache 人多了就访问非常卡的问题解决方法

    一直用WAMP 但人多了(在线人数上了500) 就卡得不得了 而这时服务器负载却很小 CPU15% 内存25% 整了好久都没个结果 偶然看到一篇教程 原来是连接数限制的问题 改了就速度飞快了 打开ap ...

  5. 51 arm x86 的大小端记录

    51 是大端模式 arm的cortex m 默认小端,可以设置大端 x86是小端 大端模式:低位字节存在高地址上,高位字节存在低地址上  小端模式:高位字节存在高地址上,低位字节存在低地址上

  6. 你所不知道的printf函数

    #include <stdio.h> int main(void) { int a = 4; int b = 3; int c = a / b; float d = *(float *)( ...

  7. Liunx - 命令整理

    ## Liunx 常用命令 ## ## 注意,在Linux中,文件没有创建时间. 1. ls : 查看当前文件夹下的所有文件 2. mkdir -- 创建一个新的文件夹 - mkdir 参数 文件名 ...

  8. php流程控制之if else语法

    php流程控制之if else语法 if和else语法 这是一个非常重要的章节,也是PHP当中的一个重要的语法. [注意]我对这个语法的定义级别为:默写级别.也就是你需要,闭着眼睛,都能够写出来的东西 ...

  9. Dao的扩展

    题目: 1.查询所有学生记录,包含年级名称2.查询S1年级下的学生记录 一.项目目录 package com.myschool.entity; import java.util.ArrayList; ...

  10. mysql更新数据,条件为实时查询出来的数据

    --将更新条件保存到临时表里 CREATE TABLE tmp3 AS (SELECT username FROM oa_user WHERE username NOT IN (SELECT user ...