Spring Boot 引入自定义yml



喜欢yml配置文件格式的人性化,也喜欢properties配置文件管理方式的人性化,
那么下面我们就来看一下 yml 是如何配置和使用类似properties管理方式的人性化。

配置文件

设置Spring Boot 系统 yml 和自定义 yml文件

application.yml


spring:
  profiles:
    active: dev
    include: test  #或者 include: "test"
  application:
    name: test-yml-application

application-test.yml


test:
  msg: 这不就是配置文件的内容吗

基于抽象类的使用

常见的有两种方式

@Value

AbstractCp


public abstract class AbstractCp {

    @Value("${test.msg}")
    private String msg;

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

}

Cp


@Component
public class Cp extends AbstractCp {

}

@ConfigurationProperties

AbstractCp


@EnableConfigurationProperties
@ConfigurationProperties("test")
public abstract class AbstractCp {

    private String msg;

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

}

Cp


@Component
public class Cp extends AbstractCp {

}

原文地址:https://www.cnblogs.com/ljmatlight/p/8328467.html

时间: 2024-08-26 02:15:45

Spring Boot 引入自定义yml的相关文章

Spring Boot通过application.yml配置文件获取属性及类信息

实体类信息Spring Boot通过application.yml配置文件获取属性及类信息 原文地址:https://blog.51cto.com/6000734/2354529

spring boot 引入模板

今天主要说下,配置在resources文件中的内容怎样被spring boot所引用. 引用静态模板的值 thymeleaf和spring boot的整合,及相关配置 根据springboot 推荐的格式,我们知道放在resources 都是一些资源,包括整个项目的配置啊 ,自定义配置啊  ,静态资源文件 (js,css等),以properties结尾.字面意思就是属性暂且就这么翻译吧. application.properties是项目资源的配置,比如jar包的配置啊,比如默认端口是8080

小代学Spring Boot之自定义Starter

想要获取更多文章可以访问我的博客?-?代码无止境. 使用Spring Boot框架一段时间之后的小代同学,发现在Spring Boot项目中经常会引入各种各样的Starter,例如Web项目的spring-boot-starter-web以及集成MyBatis时的mybatis-spring-boot-starter.那么这个Starter到底是些什么呢? 什么是Starter 经过一番研究,小代同学了解到Starter主要是Spring Boot用来简化项目依赖的一种形式,比如spring-b

Spring Boot 引入org.springframework.boot.SpringApplication出错

今天新建的一个spring boot maven项目, 写启动类时发现无法引入SpringApplication, 经查原来是冲突了,我早些时候用了比较低版本的spring boot创建了项目 ,导致电脑用户文件夹下有旧版本的配置,现在用新的版本导致了冲突 ,那么只要删除冲突的老版本,文件夹地址 C:\Users\hanfuqingshi\.m2\repository\org\springframework\boot\spring-boot 下的2.0.0.RELEASE为新版本,而其它的为老版

spring boot项目自定义数据源,mybatisplus分页、逻辑删除无效解决方法

Spring Boot项目中数据源的配置可以通过两种方式实现: 1.application.yml或者application.properties配置 2.注入DataSource及SqlSessionFactory两个Bean 通过第二种方式配置数据源则按照MybatisPlus官方文档使用分页及逻辑删除插件会无效,解决思路是在初始化SqlSessionFactory将插件设置进去 /** * 逻辑删除插件 */ @Bean public GlobalConfig globalConfig()

Spring Boot使用自定义的properties

spring boot使用application.properties默认了很多配置.但需要自己添加一些配置的时候,我们应该怎么做呢. 若继续在application.properties中添加如: wisely.name=zhangsan wisely.gender=male wisely.age=20 定义配置类: @ConfigurationProperties(prefix = "wisely") public class WiselySettings { private St

25. Spring Boot使用自定义的properties【从零开始学Spring Boot】

转:http://blog.csdn.net/linxingliang/article/details/52069515 spring boot使用application.properties默认了很多配置.但需要自己添加一些配置的时候,我们应该怎么做呢. 若继续在application.properties中添加 如: 1.     wisely2.name=wyf2 2.     wisely2.gender=male2 定义配置类: 1.     @ConfigurationPropert

Spring boot 学习 四:spring boot 配置文件 application.yml

一 关于端口: spring boot的默认端口是8080, 如果想更改的话,在配置文件中做如下配置.ServerProperties.class会去读取这个值. server: port: 8081 另外一种方法是:如果你只是想在启动的时候修改一次端口号的话,可以用命令行参数来修改端口号.配置如下:java -jar 打包之后的SpringBoot.jar  --server.port=8000 二   @RestController(spring还有另外一个注解 @Controller) 即

Spring Boot引入某个包下部分Bean

Spring Boot环境下,假如有个第三方包third.jar,内有com.xxx.config目录,在com.xxx.config目录下有3个被@Component注释的类分别是A, B, C,现在我们需要在应用中让A注册到Spring容器中,而B,C不注册进来.有2个方法: 使用@ComponetScan的Filter,类似这样:@ComponentScan(basePackageClasses = A.class, useDefaultFilters = false, includeFi