springboot实现将配置文件的属性转换成一个对应的pojo对象的属性

1.首先你需要在application.yml(如果是application.properties也差不多)配置文件配置相应的属性信息,例如

loginType:
  person: 1
  vcode: 23
2.在pom文件加入下面的依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
3.写一个相应的pojo类,注意prefix后面的需要写你自己的

@ConfigurationProperties(prefix = "myProperties.loginType")

package com.suiyu.account.properties;

import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * @author yedeguo
 * @date 2017/8/24
 */
@ConfigurationProperties(prefix = "loginType")
public class LoginTypeProperties  {
    private String person;
    private String vcode;

    public String getPerson() {
        return person;
    }

    public void setPerson(String person) {
        this.person = person;
    }

    public String getVcode() {
        return vcode;
    }

    public void setVcode(String vcode) {
        this.vcode = vcode;
    }

    public LoginTypeProperties() {
    }

    public LoginTypeProperties(String person, String vcode) {
        this.person = person;
        this.vcode = vcode;
    }

    @Override
    public String toString() {
        return "LoginTypeProperties{" +
                "person='" + person + '\'' +
                ", vcode='" + vcode + '\'' +
                '}';
    }
}

4.在springboot的入口main方法加上下面的这个注解,括号里面的写你自己的pojo类@EnableConfigurationProperties(LoginTypeProperties.class)

@SpringBootApplication
@EnableConfigurationProperties(LoginTypeProperties.class)
public class AccountApplication {

    public static void main(String[] args) {
        SpringApplication.run(AccountApplication.class, args);
    }
}
5.然后就可以在相应的Controller层或者其他地方@Autowired进来,至此大功告成

@Autowired
private LoginTypeProperties loginTypeProperties;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值