项目结构

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读取配置文件的更多相关文章

  1. SpringBoot读取配置文件源码探究

    1. SpringBoot读取配置文件源码探究 1.1. 概览 springboot的源码是再原来的Spring源码上又包了一层,看过spring源码都知道,当我们从入口debug进去的时候,原来的S ...

  2. springboot读取配置文件中的信息

    在一个项目中,我们有时候会把一些配置信息写入到一个配置文件中,在java代码中读取配置文件的信息.在此记录下读取属性文件中的内容. 在springboot项目中,springboot的配置文件可以使用 ...

  3. Springboot读取配置文件的两种方法

    第一种: application.yml配置中的参数: zip: Hello Springboot 方法读取: @RestController public class ControllerTest ...

  4. SpringBoot 读取配置文件的值 赋给静态变量

    需求:写了一个工具类,但是工具类中的一些变量需要放到配置文件中,而这个工具类中的变量与方法都是静态的,这个时候我需要一个办法将配置文件中的相关配置读取过来赋值给这些静态变量.找了一些文章,试了一些方法 ...

  5. SpringBoot读取配置文件的内容

    1.@Value读取 在springboot项目中,如果要读取配置文件application.properties或application.yml文件的内容,可以使用自带的注解@Value.以prop ...

  6. springboot读取配置文件的顺序

    前言 今天测试一些东西,发现配置文件连接的数据库一直不正常,数据也不对,今天请教了之后,原来springboot的配置文件加载不仅仅是项目内的配置文件. 正文 项目目录是这样的:文件夹下有:项目,ap ...

  7. springboot 读取配置文件

    读取配置文件 在以前的项目中我们主要在 XML 文件中进行框架配置,业务的相关配置会放在属性文件中,然后通过一个属性读取的工具类来读取配置信息. 在 Spring Boot 中我们不再需要使用这种方式 ...

  8. SpringBoot读取配置文件三步走

    1首先新建application.properties文件 cn.qdl.demo.url=http://localhost:8080 2写一个类包上面的配置文件,类名随便取 public class ...

  9. Springboot读取配置文件及自定义配置文件

    1.创建maven工程,在pom文件中添加依赖 <parent> <groupId>org.springframework.boot</groupId> <a ...

  10. SpringBoot 读取配置文件及profiles切换配置文件

    读取核心配置文件 核心配置文件是指在resources根目录下的application.properties或application.yml配置文件,读取这两个配置文件的方法有两种,都比较简单. 先创 ...

随机推荐

  1. 结对编程--基于android平台的黄金点游戏

    游戏内容: 阿超的课都是下午两点钟,这时班上不少的同学都昏昏欲睡,为了让大家兴奋起来,阿超让同学玩一个叫“黄金点”的游戏: N个同学(N通常大于10),每人写一个0~100之间的有理数 (不包括0或1 ...

  2. pyside窗口关闭触发事件

    窗口关闭事件本质上是重写了类内部的closeEvent方法,可以通过重写这个类去实现其他你想要的关闭事件. 下面的例子实现了一个简单的窗口,并为窗口添加了关闭时弹出提示框的功能. import sys ...

  3. ie的不同版本测试

    ie的11.0.9600.17728版本支持在开发者工具栏中导航中edge按钮支持测试在不同ie版本下的显示情况如ie7,8,9但不支持ie6

  4. REPEATABLE-READ下批量更新数据的问题

    1. 当前mysql的隔离级别 REPEATABLE-READ 2. 用户读取的时候或者更改的时候(通过事务)   会对 更改的数据加行锁 和 gap锁 , 最终更改完毕后 去掉锁. (行锁表示这条数 ...

  5. net reactor加密源码保软件安全-net reactor使用教程

    上一次介绍用 Xenocode Postbuild for .NET 混淆加密源代码确保软件安全,本篇将讨论用 Eziriz .NET Reactor 混淆加密软件源代码,为从未用过该软件加密的用户分 ...

  6. WeX5 - AJAX跨域调用相关知识-CORS和JSONP

    http://docs.wex5.com/ajax-cross-domain/ 1.什么是跨域 跨域问题产生的原因,是由于浏览器的安全机制,JS只能访问与所在页面同一个域(相同协议.域名.端口)的内容 ...

  7. Hibernate反向工程生成DAO

    通过Hibernate反向工程生成个DAO: package dao; import java.util.List; import org.hibernate.LockMode; import org ...

  8. 建工发债sql

    管理费用 为了得到科目名称,只好再从外面写一层 select a.*, (select b.subjname from bd_accsubj b where b.subjcode=a.scode an ...

  9. Hibernate5.2之QBC查询

                                                         Hibernate5.2值QBC查询 一.简介  Hibenate的QBC查询个人认为是Hib ...

  10. 学习C++的第三天

    1.sort函数(默认升序排序(从小到大)) 要使用此函数只需用#include <algorithm> sort即可使用,语法描述为:      sort(begin,end),表示一个 ...