zoukankan      html  css  js  c++  java
  • 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);
        }
    }
  • 相关阅读:
    三种空格unicode(u00A0,u0020,u3000)表示的区别
    python调用C++之pybind11入门(相互调用)
    基于go手动写个转发代理服务
    git rebase VS git merge
    外挂
    C#本地修改器
    C# 人工智能开源库生物特征
    深层信念网络
    ASP.NET CORE(C#)与Spring Boot MVC(JAVA)
    Net UI Spy工具:ManagedSpy
  • 原文地址:https://www.cnblogs.com/chenyanbin/p/13236903.html
Copyright © 2011-2022 走看看