springboot在原有配置文件中添加自定义配置

在实际项目开发中,经常需要使用自定义配置,本文讲解在原有配置文件中添加自定义配置;若直接自定义配置文件,请参考我的另一篇博客springboot添加自定义配置文件

在原有配置文件中添加自定义配置,有两种方式

一、第一种方式
1、自定义配置类

首先,自定义配置类,并添加注解@ConfigurationProperties,代码如下:

	package com.che.pri.properties;
	
	import org.springframework.boot.context.properties.ConfigurationProperties;
	
	@ConfigurationProperties
	public class DProperties {
	
		private String name;
		
		private String age;
	
		public String getName() {
			return name;
		}
	
		public void setName(String name) {
			this.name = name;
		}
	
		public String getAge() {
			return age;
		}
	
		public void setAge(String age) {
			this.age = age;
		}
		
	}
2、添加yml文件配置

yml文件添加如下内容

	name:  ShakeSpeare
	age:  112
3、编写controller
	package com.che.pri.controller;
	
	import org.springframework.beans.factory.annotation.Value;
	import org.springframework.web.bind.annotation.RequestMapping;
	import org.springframework.web.bind.annotation.RestController;
	
	@RestController
	@RequestMapping("/p")
	public class PropertiesController {
	
		@Value("${name}")
		private String name;
		
		@Value("${age}")
		private String age;
		
		@RequestMapping(value = "/t")
		public String test() {
			return name+","+age;
		}
	}

注意:注解@Value("${name}")可以直接拿到yml配置文件中的配置内容

4、测试

浏览器访问http://localhost:8089/p/t
这里写图片描述
原配置文件中添加自定义配置成功

二、第二种方式
1、在springboot启动类添加@EnableAutoConfiguration注解

在springboot启动类添加@EnableAutoConfiguration注解,开启对自定义配置的支持

	package com.demo;
	
	import org.springframework.boot.SpringApplication;
	import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
	import org.springframework.boot.autoconfigure.SpringBootApplication;
	@EnableAutoConfiguration
	@SpringBootApplication
	public class SpringbootPropertiesApplication {
	
		public static void main(String[] args) {
			SpringApplication.run(SpringbootPropertiesApplication.class, args);
		}
	}
2、编写配置类
	package com.demo.bean;
	
	public class User {
	
		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;
		}
		
	}
3、添加yml配置
	name: bob
	age: 123
4、编写controller
	package com.demo.controller;
	
	import org.springframework.beans.factory.annotation.Value;
	import org.springframework.web.bind.annotation.RequestMapping;
	import org.springframework.web.bind.annotation.RestController;
	
	@RestController
	public class DemoController {
	
		@Value("${name}")
		private String name;
		
		@Value("${age}")
		private int age;
		
		@RequestMapping("/demo")
		public String demo() {
			return name+age;
		}
		
	}
5、测试

浏览器访问 http://localhost:8080/demo
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

悟世君子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值