好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Springboot如何获取yml、properties参数

如何获取yml、properties参数

1、使用@Value()注解

1.1 配置数据

如:在properties.yml文件配置如下数据

?

1

2

message_zh: 张三

message_en: ergouzi

在controller中获取:

1.2 读取数据

读取自定义文件:须加注解

?

1

@PropertySource (value = { "classpath:config.yml" , "classpath:config.properties" })

读取application文件不需要加注解 

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

// 中文

@Value ( "${message_zh}" )

private String message_zh;

// 英文

@Value ( "${message_en}" )

private String message_en;

@RequestMapping (value = "/{id}" )

public String index(HttpServletRequest request, @PathVariable Integer id){

    if (id == 1 ){

        request.setAttribute( "info" ,message_zh);

    } else {

        request.setAttribute( "info" , message_en);

    }

    return "index" ;

}

2、使用 @component

?

1

2

@ConfigurationProperties (prefix = "user" )

@PropertySource (value = "classpath:myConfig.properties" )

首先在myConfig.properties或myConfig.yml中配置参数:

?

1

2

user.userName = '李二狗'

user.password = 'admin'

2.1 javabean

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

/**

  * 〈一句话功能简述〉<br> 

  * 〈yml或properties配置参数〉

  *

  * @author 丶Zh1Guo

  * @create 2018/11/21

  * @since 1.0.0

  */

@Component                                  // 组件

@ConfigurationProperties (prefix = "user" )              // 前缀

@PropertySource (value = "classpath:myConfig.properties" )     // 自定义配置文件路径

public class properConfig {

    private String userName;    // 注意要和配置文件一致

    private String password;

    public String getUserName() {

        return userName;

    }

    public void setUserName(String userName) {

        this .userName = userName;

    }

    public String getPassword() {

        return password;

    }

    public void setPassword(String password) {

        this .password = password;

    }

}

2.2 controller

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

/**

  * 〈一句话功能简述〉<br> 

  * 〈〉

  *

  * @author 丶Zh1Guo

  * @create 2018/11/21

  * @since 1.0.0

  */

@restController

public class template {

    @Autowired

    properConfig config;

    @RequestMapping (value = "/config" )

    public String config(){

        return config.getUserName();

    }

}

总结:

第一种方法适合只取某些数据

第二种方法适合取所有数据

yml和properties区别

yml:key:(空格)value

properties: key = value

配置文件读取yml自定义参数(亲测可用)

?

1

2

3

dict:

   js:

    url: D:\jsFile\

首先自定义一个参数

?

1

2

3

4

5

6

7

@Component

@Data

@ConfigurationProperties (prefix = "dict.js" )

@PropertySource (value = "classpath:application-dev.yml" )

public class PropertisParam {

    private String url;

}

利用平时@value 获取值

然后在所需要的调用的配置类里面注入PropertisParam,利用@PostConstruct初始化值

?

1

2

3

4

5

6

7

@Resource

private PropertisParam param;

private static String root= null ;

@PostConstruct

public void init(){

    root = param.getUrl();

}

另一种方式   

?

1

2

3

4

5

6

7

8

@Data

@Component

@ConfigurationProperties (prefix = "spring" )

public class LoginBody {

    private String appid;

    private String apiCode;

    private String userName;

}

基本写法就不解释了:主要讲一哈注入方式

类上面添加@component

?

1

2

3

4

5

private static LoginBody loginBody;

@Resource

public void init(LoginBody loginBody) {

    SecurityUtil.loginBody = loginBody;

}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

原文链接:https://www.cnblogs.com/wangzh1guo/p/9995248.html

查看更多关于Springboot如何获取yml、properties参数的详细内容...

  阅读:17次