标签:autowired bsp pre property sys one 生效 IV val
获取配置配置文件的值
可以使用默认值:只要配置文件配置了,哪怕为空,默认值不会生效
name=adsj
appName=this is ${name}
#sb.ip=127.0.0.1
sb.port=8080
@Component public class MyConfig { @Autowired private Environment environment; @Value("${sb.ip:localhost}") private String ip; @Value("${sb.port}") private Integer port; @Value("${name}") private String name; @Value("${appName}") private String appName; /** * tomcat.prot如果找不到就使用默认值 */ @Value("${tomcat.prot:9090}") private String tomcatprot; public void show() { System.out.println("environment = " + environment.getProperty("sb.ip")); System.out.println("environment = " + environment.getProperty("sb.ip的默认值","localhost")); System.out.println("ip = " + ip); System.out.println("port = " + port); System.out.println("name = " + name); System.out.println("appName = " + appName); System.out.println("tomcatprot = " + tomcatprot); }
标签:autowired bsp pre property sys one 生效 IV val
原文地址:https://www.cnblogs.com/songfahzun/p/9240719.html