SpringBoot注解配置文件映射属性和实体类

时间:2020-07-04 22:24:32   收藏:0   阅读:94

配置文件加载

方式一

pay.properties

# 微信支付的appid
wxpay.appid=w23232323
# 支付密钥
wxpay.sercret=abd
# 微信支付商户号
wx.mechid=1234

TestController.java

package net.cyb.demo.controller;

import net.cyb.demo.utils.JsonData;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/test")
@PropertySource({"classpath:pay.properties"})
public class TestController {
    @Value("wxpay.appid")
    private String payAppid;
    @Value("wxpay.sercret")
    private String paySecret;
    @GetMapping("get_config")
    public JsonData testConfig(){
        Map<String,String> map=new HashMap<>();
        map.put("appid",payAppid);
        map.put("secret",paySecret);
        return JsonData.buildSuccess(map);
    }
}

方式二

pay.properties

# 微信支付的appid
wxpay.appid=w23232323
# 支付密钥
wxpay.sercret=abd
# 微信支付商户号
wx.mechid=1234

WXConfig.java

package net.cyb.demo.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import java.io.Serializable;

@Configuration
@PropertySource({"classpath:pay.properties"})
public class WXConfig implements Serializable {
    @Value("wxpay.appid")
    private String payAppid;
    @Value("wxpay.sercret")
    private String paySecret;
    @Value("wx.mechid")
    private String payMechId;

    public String getPayAppid() {
        return payAppid;
    }

    public void setPayAppid(String payAppid) {
        this.payAppid = payAppid;
    }

    public String getPaySecret() {
        return paySecret;
    }

    public void setPaySecret(String paySecret) {
        this.paySecret = paySecret;
    }

    public String getPayMechId() {
        return payMechId;
    }

    public void setPayMechId(String payMechId) {
        this.payMechId = payMechId;
    }
}

TestController.java

package net.cyb.demo.controller;

import net.cyb.demo.config.WXConfig;
import net.cyb.demo.utils.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/test")
public class TestController {
    @Autowired
    private WXConfig wxConfig;
    @GetMapping("get_config")
    public JsonData testConfig(){
        Map<String,String> map=new HashMap<>();
        map.put("appid",wxConfig.getPayAppid());
        map.put("secret",wxConfig.getPaySecret());
        map.put("mechid",wxConfig.getPayMechId());
        return JsonData.buildSuccess(map);
    }
}

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!