配置变量/配置数组/比较配置文件/多情况配置/服务器笔记 ...

金歌  金牌会员 | 2024-8-12 09:43:07 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 916|帖子 916|积分 2748


  • 配置变量
演示错误操纵:

  • 在application.yml文件中
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/t311
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    map-underscore-to-camel-case: false  #使用驼峰命名法


#配置变量
username: 罗学敏



  • 在controller层
@RestController
public class TestController {

    @Value("${username}")
    private String userName;

    @GetMapping("getUN")
    public String getUserName(){
        return userName;
    }
}



原因:   username会其它同名,运行后会出现本机的名称
所以取一个不与username同名即可

正确操纵:
#配置变量
usernames: 罗学敏


@RestController
public class TestController {

    @Value("${usernames}")
    private String userName;

    @GetMapping("getUN")
    public String getUserName(){
        return userName;
    }
}




二、配置数组
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/t311
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    map-underscore-to-camel-case: false  #使用驼峰命名法


#配置变量
usernames: 罗学敏

#配置数组
#指定前缀,只有有一个
my:
  arr: [1,2,3]
  address:
    - 益阳
    - 长沙
    - 浏阳


在创建一个实体类
package com.entity;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * 配置文件的注入
 */
@Component
@ConfigurationProperties(prefix = "my")  //添加前缀
@Data
public class My {
    private String[] address;

    private int[] arr;
}


在controller层
@RestController
@Slf4j //日志操纵
public class TestController {

    @Value("${usernames}")
    private String userName;

    @Autowired
    public My my;  //创建一个对象

    @GetMapping("getUN")
    public String getUserName(){
        log.info(my.toString());
        return userName;
    }
}


运行后



三、比较配置文件
1.Application.properties (1)配置文件 优先于 Application.yml (2)配置文件

2.在resources下创建一个config包(3)   优先于(1) (2)配置文件

运行后


3.在项目下面创建(4)   优先于(1) (2) (3)配置文件



4.在项目下面创建(5)  优先于(1) (2) (3) (4)配置文件


5.在app运行点击下面(6)  优先于(1) (2) (3) (4) (5)配置文件



-Dserver.port=14101



  • 多情况配置

就会出现
profiles:
  active: lxm



就会出现
spring:
  config:
    activate:
      on-profile: lxm


团体如下:
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/t311
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
  profiles:
    active: lxm
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    map-underscore-to-camel-case: false  #使用驼峰命名法

#配置数组
#指定前缀,只有有一个
my:
  arr: [1,2,3]
  address:
    - 益阳
    - 长沙
    - 浏阳

server:
  port: 10000

--- #区别不同的配置块/不同情况
spring:
  config:
    activate:
      on-profile: lxm
#配置变量
usernames: 罗学敏

---
spring:
  config:
    activate:
      on-profile: lw
#配置变量
usernames: 刘伟







如果更改activate




添加配置文件如下:




在app运行中添加

  -Dspring.profiles.active=lw


                                      


  • 服务器
除了常用的Tomcate 还有jetty  jboss  weblogic
使用jetty服务器
1.添加依靠

运行后

      


                   


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

金歌

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表