SpringBoot获得application.properties中的自定义配置参数

本文介绍了 springboot中如何获取自定义配置参数

application.properties文件配置信息如下:

server.port=8088
server.servlet.context-path=/springboot-ActiveMQ/
spring.activemq.broker-url=tcp://localhost:61616

#自定义属性
url=http://127.0.0.0:8899

编写一个GetPropertiesController测试类

package com.test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GetPropertiesController {

  @Autowired  
    private Environment environment; 
	
	//http://localhost:8088/springboot-ActiveMQ/getProperties
	
	@GetMapping("/getProperties")
	public void getProperties() {
	//获取项目端口号server.port=8088
	System.out.println("项目端口号为:"+environment.getProperty("server.port"));
	//获取activemq路径spring.activemq.broker-url=tcp://localhost:61616
	System.out.println("获取activemq路径为:"+environment.getProperty("spring.activemq.broker-url"));
	//获取自定义属性url=http://127.0.0.0:8899
	System.out.println("获取自定义属性路径为:"+environment.getProperty("url"));
	}
	
}

  

启动项目访问http://localhost:8088/springboot-ActiveMQ/getProperties可以看到控制台输出

项目端口号为:8088
获取activemq路径为:tcp://localhost:61616
上一篇 下一篇


推荐文章

评论
说点什么吧?

发表评论

取消回复
  最新文章