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配置文件,读取这两个配置文件的方法有两种,都比较简单. 先创 ...
随机推荐
- 2016HUAS_ACM暑假集训4M - 基础DP
简单的0-1背包问题,大家都会做的.题意不想解释太多. 简述题目的案例及以几个关键 Sample Input 1 //测试组数T 5 10 ...
- Android AsyncTask 简单用法
简介 AsyncTask 是一个轻量级的异步处理类.使用是需继承自该类.可以方便的执行异步任务并且在将进度显示在UI上. 注意事项 AsyncTask只适合处理轻量级的任务即耗时几秒或者几十秒的任务. ...
- 安装Linux 16.04 时,选择好分区后,进到选择地点的界面后,总是闪退,退到最原始的界面
这两天装 Linux 系统,总是遇到一个很蛋疼的问题: 当你累死累活把分区什么的都设置好了之后,在输入了系统名字,开机密码那几项之后,再选择地点的时候(如:选择 "上海"),然后就 ...
- 嵌套循环中break、continue的用法
在循环语句中经常会用到break.continue语句,基本用法如下: 一.break语句为退出当前循环,在嵌套循环中结果如下: var num=0; for(var i=0;i<5;i++){ ...
- Kylin(三): Saiku
Saiku是一个轻量级的OLAP分析引擎,可以方便的扩展.嵌入和配置.Saiku通过REST API连接OLAP系统,利用其友好的界面为用户提供直观的分析数据的方式,它是基于jQuery做的前端界面. ...
- MyBatis学习(三)、动态SQL语句
三.动态SQL语句 有些时候,sql语句where条件中,需要一些安全判断,例如按某一条件查询时如果传入的参数是空,此时查询出的结果很可能是空的,也许我们需要参数为空时,是查出全部的信息.使用Orac ...
- 学会使用notepad++
官网地址:https://notepad-plus-plus.org/ 字体尺寸更改:ctrl+鼠标滚轮 主题:设置-语言格式设置,推荐Obsidian或者Zenburn主题,推荐Consolas 1 ...
- Alembic
Layers of the Library: AbcA(low) --> Abc --> AbcGeom(high) Container Hierarchy in Alembic ...
- NC57银行档案和客商银行账号为建行04 UPDATE
第二步 银行档案 update bd_bankdoc set bankdoccode='04N'|| bankdoccode , pk_banktype='0001ZZ1000000001OCUD' ...
- modelsim操作流程
1.File->Change Directory2.File->New->Library 在弹出的对话框中选择 a new library and a logical mapping ...