SpringBoot读取配置文件
项目结构

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>test.demo</groupId>
<artifactId>springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>springboot</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.0.RELEASE</version>
<relativePath />
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
@Configurationproperties注解和@Value注解
@ConfigurationProperties加在有@Configuration的类或者由@Bean注解的方法上,
另外一个类似的注解@EnableConfigurationProperties
用于允许@ConfigurationProperties
使用示例:
package hello; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class App
{
public static void main(String[] args)
{
SpringApplication.run(App.class, args);
}
}
package hello.bean; //@Configuration
//@ConfigurationProperties("user.properties")
public class TestBean
{
private String name;
private int age; public String getName()
{
return name;
} public void setName(String name)
{
this.name = name;
} public int getAge()
{
return age;
} public void setAge(int age)
{
this.age = age;
}
}
package hello.config; import hello.bean.TestBean; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class BeanConfig
{
@ConfigurationProperties("user.properties")
@Bean
public TestBean getBean()
{
return new TestBean();
} }
package hello.controller; import hello.bean.TestBean; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; @RestController
public class Controller
{
@Autowired
TestBean bean;
@Value("${user.properties.name}")
String name; @RequestMapping(value = "/test", method = RequestMethod.GET)
public String get()
{
System.out.println(name);
return bean.getName();
} @RequestMapping(value = "/post", method = RequestMethod.POST)
public String post()
{
return "post";
} }
application.yml
user:
properties:
age: 18
name: zzzz
单个属性使用@Value
SpringBoot读取配置文件的更多相关文章
- SpringBoot读取配置文件源码探究
1. SpringBoot读取配置文件源码探究 1.1. 概览 springboot的源码是再原来的Spring源码上又包了一层,看过spring源码都知道,当我们从入口debug进去的时候,原来的S ...
- springboot读取配置文件中的信息
在一个项目中,我们有时候会把一些配置信息写入到一个配置文件中,在java代码中读取配置文件的信息.在此记录下读取属性文件中的内容. 在springboot项目中,springboot的配置文件可以使用 ...
- Springboot读取配置文件的两种方法
第一种: application.yml配置中的参数: zip: Hello Springboot 方法读取: @RestController public class ControllerTest ...
- SpringBoot 读取配置文件的值 赋给静态变量
需求:写了一个工具类,但是工具类中的一些变量需要放到配置文件中,而这个工具类中的变量与方法都是静态的,这个时候我需要一个办法将配置文件中的相关配置读取过来赋值给这些静态变量.找了一些文章,试了一些方法 ...
- SpringBoot读取配置文件的内容
1.@Value读取 在springboot项目中,如果要读取配置文件application.properties或application.yml文件的内容,可以使用自带的注解@Value.以prop ...
- springboot读取配置文件的顺序
前言 今天测试一些东西,发现配置文件连接的数据库一直不正常,数据也不对,今天请教了之后,原来springboot的配置文件加载不仅仅是项目内的配置文件. 正文 项目目录是这样的:文件夹下有:项目,ap ...
- springboot 读取配置文件
读取配置文件 在以前的项目中我们主要在 XML 文件中进行框架配置,业务的相关配置会放在属性文件中,然后通过一个属性读取的工具类来读取配置信息. 在 Spring Boot 中我们不再需要使用这种方式 ...
- SpringBoot读取配置文件三步走
1首先新建application.properties文件 cn.qdl.demo.url=http://localhost:8080 2写一个类包上面的配置文件,类名随便取 public class ...
- Springboot读取配置文件及自定义配置文件
1.创建maven工程,在pom文件中添加依赖 <parent> <groupId>org.springframework.boot</groupId> <a ...
- SpringBoot 读取配置文件及profiles切换配置文件
读取核心配置文件 核心配置文件是指在resources根目录下的application.properties或application.yml配置文件,读取这两个配置文件的方法有两种,都比较简单. 先创 ...
随机推荐
- jsp中查询条件的回显
后台框架为ssh,前台纯手写无框架是最老的写法,因为是接手别人的项目无法改变框架原型,只能基于修改. 进入正题: 我这里查询条件有两种input的text(文本框)和select(下拉框). 1.te ...
- MongoDB安装及添加到Windows服务,随系统启动
本文介绍在Windows环境下安装MongoDB及添加到Windows服务中,随系统启动 首先去官网下载Windows安装包:https://www.mongodb.org/downloads 一般情 ...
- 【以前弄的老东西】DLLspy超犀利后门 (源代码+程序+使用手册+二次开发文档)
这个玩意儿是很久之前的整的.一直没有做完,但是基本功能和框架都做好了,现在发出来,希望有能力的家伙一起完成.DLLspy,绝对免杀,隐藏,HTTP请求劫持,居家旅行,杀人放火必备良药.有时间我会继续开 ...
- Saddest's polar bear Pizza offered new YorkShire home
Saddest:adj,可悲的,悲哀的,polar,两级的,极地额,YorkShire,约克郡 A UK wildlife park has confirmed that it is offering ...
- unity, 只发射一个粒子的粒子系统
- openssl 升级
openssl version -a rpm -q --changelog openssl | grep CVE bash -version #!/bin/bash if [[ $EUID -ne ...
- JavaScript使用XMLHttpRequest 發送GET/Post 請求
<!DOCTYPE HTML> <html> <head> <title>Demo</title> <script type=&quo ...
- coffeeScript中类的多态[学习篇]
类的一大应用就是多态.多态是一个面向对象编程的高级术语----“一个东西可编程很多不同的东西,但不是任何东西”.[引自coffeescript深入浅出] class Shape constructor ...
- .NET设计规范————命名规范
NET设计规范:约定.惯用法与模式———命名规范 前言: 最近在看<.NET设计规范:约定.惯用法与模式>一书,主要还是讲.NET的设计规范,以前对这一块也不是特别在意, ...
- System.Data.OleDb操作access数据库类,【bubuko.com】
access数据库在应用了System.Data.OleDb后操作会很方便,这是一个常用的数据库操作类,其中两个方法,一个是返回datatable的,一个是执行sql语句返回影响记录的(一般是inse ...