zoukankan      html  css  js  c++  java
  • java 加载并读取Properties 文件

    1 .系统自带的application.properties  (以下代码仅供参考,不能粘贴复制)

      假设application.properties文件有下面两个值:

          come.test.name = chen

          come.test.age = 18

          come.test.phone = 18795812345

      直接在类中的成员变量上添加注解@Value("${ }"),如下

         public class Test(){

          @Value("${come.test.name}")

           public  String name;

          @Value("${come.test.age}")

           public  String age;   

          @Value("${come.test.phone}")

           public  String phone;

          public  static  String getProperties(){

            String value = "name"  + name + "age" + age + "phone" + phone;

            return value;

          }

        }

      注意:上面的类Test已经读取加载properties文件,如果你还想在别的类中引用这个Test类。一定在Test类上面加上@Component 

                     @Component

         public class Test(){

          @Value("${come.test.name}")

           public  String name;

          @Value("${come.test.age}")

           public  String age;   

          @Value("${come.test.phone}")

           public  String phone;

          public  static  String getProperties(){

            String value = "name"  + name + "age" + age + "phone" + phone;

            return value;

          }

        }

     Controller层的类引用Test; 

       @RestController

       public class  Demo{

         @Autowired

          Test test;

        public  String getPro(){

            return  test.getProperties();

          }

       }

    2 . 如果你是自定义的properties文件。只需要在Test类加上注解 @PropertySource("classpath:xxx.properties")

        @Component

        @PropertySource("classpath:xxx.properties")

         public class Test(){

          @Value("${come.test.name}")

           public  String name;

          @Value("${come.test.age}")

           public  String age;   

          @Value("${come.test.phone}")

           public  String phone;

          public  static  String getProperties(){

            String value = "name"  + name + "age" + age + "phone" + phone;

            return value;

          }

        }

  • 相关阅读:
    jQuery中的一些操作
    laravel使用消息队列
    Laravel的开发环境Homestead的搭建与配置
    python爬虫学习
    配置文件
    sql根据时间差查询数据
    Oracle根据连接字符串获取库下的表列表、获取表结构
    Sql根据连接字符串获取库下的表列表、获取表结构
    判断网络连接
    线程锁,解决多线程并发问题
  • 原文地址:https://www.cnblogs.com/hope-xu/p/10744634.html
Copyright © 2011-2022 走看看