spring读取自定义配置信息

spring读取自定义配置信息,第1张

spring读取自定义配置信息 spring读取自定义配置信息



配置信息
#我的配置项
my:
  url: zhengxinghua.top
  data: 2021-12-31 14:21:00



第一种方式

@Value("${配置}")

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class SpringbootApplicationTests {

    //读取配置信息
    @Value("${my.url}")
    String  url  ;

    //读取配置星系
    @Value("${my.data}")
    String  data  ;

    @Test
    void contextLoads() {
        System.out.println();
        System.out.println("读取URL:"+url);
        System.out.println("读取Data"+data);
    }

}

第二种方式 configuration

1、pom.xml

        
        
            org.springframework.boot
            spring-boot-configuration-processor
            2.5.6
        
        
		
        
            org.projectlombok
            lombok
            true
        

2、配置

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


@Data
@Component
@ConfigurationProperties(prefix = "my")
public class Content {
	//这里的名字和配置一致
    private  String url ;
    private  String data ;
}

3、输出

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class SpringbootApplicationTests {

    //读取配置信息
    @Autowired
    Content content ;



    @Test
    void readContent() {
        System.out.println();
        System.out.println("读取URL:"+content.getUrl());
        System.out.println("读取Data"+content.getData());
    }



}

结果






欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5691043.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存