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


    配置文件加载

    方式一

    • Controller上面配置@PropertySource({"classpath:pay.properties"})
    • 添加属性@Value("wxpay.appid") private String payAppid;

    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);
        }
    }

    方式二

    • 添加@Component注解
    • 使用PropertySource注解指定配置文件位置
    • 使用@ConfigurationProperties注解,设置相关属性
    • 必须通过注入IOC对象Resource进来,才能在类中使用获取的配置文件值。@Autowired private WXConfig wxConfig;

    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);
        }
    }
  • 相关阅读:
    一个列表如何根据另一个列表进行排序(数组的相对排序)
    汉诺塔问题
    python面向对象基础
    python爬虫
    软件开发目录规范
    python--->包
    编译python文件
    python文件的俩种用途
    python模块的搜索路径
    python 循环导入的问题
  • 原文地址:https://www.cnblogs.com/chenyanbin/p/13236903.html
Copyright © 2020-2023  润新知