首页 > 其他分享 >注解@ConfigurationProperties使用方法

注解@ConfigurationProperties使用方法

时间:2023-01-06 09:24:12浏览次数:39  
标签:map spring import springframework ConfigurationProperties private org 注解 方法

1、配置文件内容

spring.datasource.url=jdbc:mysql://localhost:3306/satellite_resource?characterEncoding=utf8&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

2、创建类

复制代码
package com.example.demo.user;


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

@ConfigurationProperties(prefix = "spring.datasource")
@Component
@Data
public class DatasourcePro {

    private String url;

    private String username;

    private String password;

    // 配置文件中是driver-class-name, 转驼峰命名便可以绑定成
    private String driverClassName;


}
复制代码

3、用法

复制代码
package com.example.demo.user;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping(value = "/config")
public class ConfigurationPropertiesController {

    @Autowired
    private DatasourcePro datasourcePro;

    @GetMapping("/test")
    public Map<String, Object> test() {

        Map<String, Object> map = new HashMap<>();
        map.put("url", datasourcePro.getUrl());
        map.put("userName", datasourcePro.getUsername());
        map.put("password", datasourcePro.getPassword());
        map.put("className", datasourcePro.getDriverClassName());

        return map;
    }
}
复制代码

4、结果

访问:http://localhost:9110/config/test

复制代码

{
"password": "123456",
"className": "com.mysql.cj.jdbc.Driver",
"userName": "root",
"url": "jdbc:mysql://localhost:3306/satellite_resource?characterEncoding=utf8&serverTimezone=Asia/Shanghai"
}

 

标签:map,spring,import,springframework,ConfigurationProperties,private,org,注解,方法
From: https://www.cnblogs.com/kn-zheng/p/17029414.html

相关文章

  • 关于DoTween的使用方法笔记
    一、Unity常用组件拓展方法(1)Transform拓展方法1)Position1)改变世界坐标移动方法,第一个参数是要移动到的目标点,不是移动这个向量的距离transform.DOMove(newVector3(1......
  • SpringBoot整合Caffeine(通过注解)
    一、了解缓存配置先来了解一下配置方法吧,SimpleCacheManager和CaffeineCacheManager配置的区别:SimpleCacheManager: 这种缓存管理器允许你在应用程序启动时通过配置......
  • 8.注解开发
    对于简单的数据库操作可以使用注解开发,对于复杂的数据库操作不推荐使用!!mybatis的常用注解:@Insert:新增@Update:更新@Delete:删除@Select:查询@Result:实现结果集封......
  • Thread 之 run() 方法
    案例代码一@Slf4jpublicclassClient{publicstaticvoidmain(String[]args){MyThreadmyThread=newMyThread();myThread.start();......
  • BST查找结构与折半查找方法的实现与实验比较
    简介作业:查找结构与排序方法作业题目:BST查找结构与折半查找方法的实现与实验比较要求编写程序实现BST存储结构的建立(插入)、删除、查找和排序算法;实现折半查找算法......
  • JQuery:常用方法一览
    代码CodehighlightingproducedbyActiproCodeHighlighter(freeware)http://www.CodeHighlighter.com/-->Attribute:$(”p”).addClass(css中定义的样式类型);给某个元......
  • 三位毫秒数产生随机数方法
    #1三位随机数是获取电子硬件的毫秒级时间戳,从后到前的读取三个数字,把它们组合成三位数。(例如毫秒级时间戳1672673902720,获得的三位数是027。毫秒级时间戳是一个十多位的......
  • 关于爬虫中几个常用库的使用方法总结
    关于爬虫中几个常用库的使用方法总结学了半个多月的爬虫了,用这个案例总结一下各个爬虫库的用法。当然后面还有更重要和更好用的方法,再等后面学到,再做总结了。1.目标......
  • Java中next() 、nextInt() 和 nextLine() 方法
    Scanner的几个常用next输入方法要点next():一直接收从键盘中打入的内容直到读取到回车,此回车并不会被读取,且一定要读取到有效字符后才可以结束输入。对输入有效字符之......
  • Thread 之 start() 方法
    案例代码@Slf4jpublicclassClient{publicstaticvoidmain(String[]args){Threadt1=newThread("t1"){@Overridepu......