开发者社区> 问答> 正文

ConfigurationProperties注解读取方式是什么?

ConfigurationProperties注解读取方式是什么?

展开
收起
cuicuicuic 2021-11-03 00:12:39 349 0
1 条回答
写回答
取消 提交回答
  • @ConfigurationProperties方式

    自定义配置类:PropertiesConfig.java

    package com.zyd.property.config;

    import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;

    import org.springframework.boot.context.properties.ConfigurationProperties; //import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component;

    /** * 对应上方配置文件中的第一段配置 * @author yadong.zhang * @date 2017年6月1日 下午4:34:18 * @version V1.0 * @since JDK : 1.7 */ @Component @ConfigurationProperties(prefix = "com.zyd") // PropertySource默认取application.properties // @PropertySource(value = "config.properties") public class PropertiesConfig {

    public String type3;
    public String title3;
    public Map<String, String> login = new HashMap<String, String>();
    public List<String> urls = new ArrayList<>();
    
    public String getType3() {
        return type3;
    }
    
    public void setType3(String type3) {
        this.type3 = type3;
    }
    
    public String getTitle3() {
        try {
            return new String(title3.getBytes("ISO-8859-1"), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return title3;
    }
    
    public void setTitle3(String title3) {
        this.title3 = title3;
    }
    
    public Map<String, String> getLogin() {
        return login;
    }
    
    public void setLogin(Map<String, String> login) {
        this.login = login;
    }
    
    public List<String> getUrls() {
        return urls;
    }
    
    public void setUrls(List<String> urls) {
        this.urls = urls;
    }
    

    } 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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 程序启动类:Applaction.java

    package com.zyd.property;

    import java.io.UnsupportedEncodingException; import java.util.HashMap; import java.util.Map;

    import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;

    import com.zyd.property.config.PropertiesConfig;

    /** * @author yadong.zhang * @date 2017年6月1日 下午3:49:30 * @version V1.0 * @since JDK : 1.7 */ @SpringBootApplication @RestController public class Applaction {

    @Autowired
    private PropertiesConfig propertiesConfig;
    
    /**
     * 
     * 第一种方式:使用`@ConfigurationProperties`注解将配置文件属性注入到配置对象类中
     * 
     * @author zyd
     * @throws UnsupportedEncodingException
     * @since JDK 1.7
     */
    @RequestMapping("/config")
    public Map<String, Object> configurationProperties() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("type", propertiesConfig.getType3());
        map.put("title", propertiesConfig.getTitle3());
        map.put("login", propertiesConfig.getLogin());
        map.put("urls", propertiesConfig.getUrls());
        return map;
    }
    
    public static void main(String[] args) throws Exception {
        SpringApplication application = new SpringApplication(Applaction.class);
        application.run(args);
    }
    

    } 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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 访问结果:

    {"title":"使用@ConfigurationProperties获取配置文件","urls":["http://ztool.cc","http://ztool.cc/format/js","http://ztool.cc/str2image","http://ztool.cc/json2Entity","http://ztool.cc/ua"],"login":{"username":"zhangdeshuai","callback":"http://www.flyat.cc","password":"zhenshuai"},"type":"Springboot - @ConfigurationProperties"}

    2021-11-03 08:46:09
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载