转载:https://www.imooc.com/article/18252
一、@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 <a href="mailto:yadong.zhang0415@gmail.com">yadong.zhang</a> * @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; } } 程序启动类: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 <a href="mailto:yadong.zhang0415@gmail.com">yadong.zhang</a> * @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); } } 访问结果: {"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"} 二、使用@Value注解方式 程序启动类:Applaction.java package com.zyd.property; import java.io.UnsupportedEncodingException; import java.util.HashMap; import java.util.Map; import org.springframework.beans.factory.annotation.Value; 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; /** * @author <a href="mailto:yadong.zhang0415@gmail.com">yadong.zhang</a> * @date 2017年6月1日 下午3:49:30 * @version V1.0 * @since JDK : 1.7 */ @SpringBootApplication @RestController public class Applaction { @Value("${com.zyd.type}") private String type; @Value("${com.zyd.title}") private String title; /** * * 第二种方式:使用`@Value("${propertyName}")`注解 * * @author zyd * @throws UnsupportedEncodingException * @since JDK 1.7 */ @RequestMapping("/value") public Map<String, Object> value() throws UnsupportedEncodingException { Map<String, Object> map = new HashMap<String, Object>(); map.put("type", type); // *.properties文件中的中文默认以ISO-8859-1方式编码,因此需要对中文内容进行重新编码 map.put("title", new String(title.getBytes("ISO-8859-1"), "UTF-8")); return map; } public static void main(String[] args) throws Exception { SpringApplication application = new SpringApplication(Applaction.class); application.run(args); } } 访问结果: {"title":"使用@Value获取配置文件","type":"Springboot - @Value"}
这是几种较为常见的方式,其中第一种,必须在需要引用的类上加上@EnableConfigurationProperties注解,配合@ConfigurationProperties一起使用