美文网首页
spring-boot 配置篇

spring-boot 配置篇

作者: hotallen | 来源:发表于2018-07-06 22:27 被阅读0次

spring boot 的配置主要包括pom、yml、xml、properties 四种,分别可以实现不同的配置功能。
pom配置沿用maven,来配置项目的依赖以及公共的属性变量,也可以利用profiles属性来实现变量的环境隔离和区分,可以利用profiles里面的properties属性来定义一些变量,可以通过在编译的时候加入-Pdev这种参数来选择不同环境的值,例子如下:

maven pom.xml配置
 <profiles>
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <spring.profiles.active>dev</spring.profiles.active>
            </properties>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <spring.profiles.active>prod</spring.profiles.active>
            </properties>
        </profile>
        <profile>
            <id>release</id>
            <properties>
                <spring.profiles.active>release</spring.profiles.active>
            </properties>
        </profile>
    </profiles>

除了在pom中配置属性变量之外,还可以在yml里面进行配置,yml里面也可以实现不同环境变量的筛选,------下面即显示不同环境的变量定义,通过spring.profiles.active 来实现环境变量的选择,需要注意的是pom里面定义的变量在yml和xml里面需要使用"@spring.profiles.active @"这种才能生效。

yml配置
appname: demo
server:
  context-path: /
  port: 8091
jwt:
  header: Authorization
  expiration: 604800
  secret: @!@!#%$%^
mybatis:
  mapper-locations: classpath:mapper/*.xml
  config-location: classpath:page-config.xml
logback:
  appname: demo
  logdir: ./
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8261/eureka/
spring:
  application:
    name: user
  profiles:
    active: @spring.profiles.active@
---
spring:
  profiles: dev
  redis:
        password: @#SDSS
        database: 0
        port: 8379
        pool:
          max-idle: 8
          min-idle: 0
          max-active: 8
          max-wait: -1
        host: 127.0.0.1
        timeout: 0
  jackson:
    serialization:
      INDENT_OUTPUT: true
  datasource:
    password: 123343
    driverClassName: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/demo?useUnicode=true&characterEncoding=UTF-8
    username: demo
---
spring:
  profiles: prod
  redis:
        password: 1swe2sw
        database: 0
        port: 8379
        pool:
          max-idle: 8
          min-idle: 0
          max-active: 8
          max-wait: -1
        host: 127.0.0.1
        timeout: 0
  jackson:
    serialization:
      INDENT_OUTPUT: true
  datasource:
    password: wew!1212#!
    driverClassName: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/demo?useUnicode=true&characterEncoding=UTF-8
    username: demo

xml里面bean的各种定义和spring的类似,只是外部变量的应用需要使用"@prams.name@"这种才能生效

xml
 <root level="warn">
        <appender-ref ref="@log.out.target@" />
        <appender-ref ref="AsyncLog" />
        <appender-ref ref="logfile"/>
    </root>

相关文章

网友评论

      本文标题:spring-boot 配置篇

      本文链接:https://www.haomeiwen.com/subject/jocuuftx.html