SpringBoot(四)---配置文件占位符
在我们配置properties文件时,可以通过文件占位符使用属性文件中的其他属性值。
举个例子:
ConfigBean.java
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 | package com.zk.myspringboot_003; 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; @Component @ConfigurationProperties(prefix = "demo" ) @PropertySource(value = { "classpath:application.properties" }) public class ConfigBean { private String name; private String age; private Map<String, String> login= new HashMap<String,String>(); private List<String> urls= new ArrayList<String>(); 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; } public String getName() { return name; } public void setName(String name) { this .name = name; } public String getAge() { return age; } public void setAge(String age) { this .age = age; } @Override public String toString() { return "ConfigBean [name=" + name + ", age=" + age + ", login=" + login + ", urls=" + urls + "]" ; } } |
SpringBootApp.java
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 | package com.zk.myspringboot_003; import java.io.IOException; import java.util.Properties; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context. event .ApplicationStartedEvent; import org.springframework.context.ApplicationListener; import org.springframework.context.annotation.ImportResource; import org.springframework.core.io.support.PropertiesLoaderUtils; @SpringBootApplication @ImportResource(locations={ "classpath:beans.xml" }) //@ImportResource:导入spring配置文件让配置文件的内容生效 public class SpringBootApp implements ApplicationListener<ApplicationStartedEvent>{ public static void main(String[]args){ SpringApplication.run(SpringBootApp. class , args); } @Override public void onApplicationEvent(ApplicationStartedEvent event ) { // TODO Auto-generated method stub Properties properties; try { properties = PropertiesLoaderUtils.loadAllProperties( "classpath:application.properties" ); System. out .println(properties); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } |
TestController.java
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 | package com.zk.myspringboot_003; import java.util.List; import java.util.Map; import java.util.Map.Entry; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.env.Environment; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class TestController { @Value( "${demo.name}" ) private String name; @Value( "${demo.age}" ) private String age; @Autowired private Environment environment; // @Autowired // private ConfigBeanValue configbeanValue; @Autowired private ConfigBean configBeanProp; @RequestMapping( "/demo" ) public String hello() { return "hello," +name+ ",you are " +age+ "demo1" ; } @RequestMapping( "/demo2" ) public String getprops(){ String name=environment.getProperty( "demo.name" ); String age=environment.getProperty( "demo.age" ); List<String> urlslist=configBeanProp.getUrls(); Map<String,String> loginmap=configBeanProp.getLogin(); for (Entry<String, String> e:loginmap.entrySet()) { System. out .println(e.getKey()+ ":" +e.getValue()); } return "hello," +name+ ".you are " +age+ "demo2\n" +urlslist.toString()+ "\n" +loginmap. get ( "username" ); } @RequestMapping( "/demo3" ) public String getBean(){ String name=configBeanProp.getName(); String age=configBeanProp.getAge(); return "hello," +name+ ".you are " +age+ "demo3" ; } } |
application.properties
1 2 3 4 5 6 7 8 9 10 11 12 | demo.name=zk${demo.age} demo.age=25${random.uuid}_age #map demo.login[username]=zhangdeshuai demo.login[password]=zhenshuai demo.login[callback]=http: //www.flyat.cc #list demo.urls[0]=http: //ztool.cc demo.urls[1]=http: //ztool.cc/format/js demo.urls[2]=http: //ztool.cc/str2image demo.urls[3]=http: //ztool.cc/json2Entity demo.urls[4]=http\: //ztool.cc/ua |
在配置文件中的占位符为
1 | ${demo.age} |
在运行代码后运行的结果如下:
分类:
SpringBoot
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)