zoukankan      html  css  js  c++  java
  • SpringBoot读取自定义配置文件

    自定义配置文件

    my-config.properties

    1 bfxy.title=bfxy
    2 bfxy.name=hello spring boot!
    3 bfxy.attr=12345

    配置文件类

    appconfig.java

     1 import org.springframework.beans.factory.annotation.Autowired;
     2 import org.springframework.beans.factory.annotation.Value;
     3 import org.springframework.context.annotation.ComponentScan;
     4 import org.springframework.context.annotation.Configuration;
     5 import org.springframework.context.annotation.PropertySource;
     6 import org.springframework.core.env.Environment;
     7 
     8 @Configuration
     9 @ComponentScan({"com.bfxy.springboot.*"})            //你要进行扫描的包路径
    10 @PropertySource("classpath:my-config.properties")    //加载指定的配置
    11 public class appConfig {
    12     
    13     @Value("${custom.group}")
    14     private String customGroup;
    15     
    16     @Autowired
    17     private Environment environment;
    18     
    19     @Value("${bfxy.name}")
    20     private String name;
    21     
    22     @Value("${bfxy.title}")
    23     private String title;
    24     
    25     @Value("${bfxy.attr}")
    26     private String attr;
    27     
    28     
    29     public void output(){
    30         System.err.println("通过@Value注解读取配置: " + this.customGroup);
    31         System.err.println("通过Environment对象读取配置: " + environment.getProperty("custom.team"));
    32         
    33         System.err.println("加载自己的配置文件内容"+this.name+","+this.title+","+this.attr);
    34     }
    35     
    36 
    37 }
  • 相关阅读:
    t
    [持续更新]android stduio的一些小技巧
    Launcher2编译
    数据库
    JavaWeb--会话与状态管理2--cookie 显示最近浏览商品
    JavaWeb--会话与状态管理1--cookie 基础与自动登录
    JavaWeb--MVC案例1-------(6)修改
    JavaWeb--MVC案例1-------(5)添加
    JavaWeb--MVC总结
    JavaWeb--MVC案例1-------(4)删除
  • 原文地址:https://www.cnblogs.com/yangyang521/p/10287079.html
Copyright © 2011-2022 走看看