spring boot的基本配置——spring boot的全局配置文件——读取应用配置——@ConfigurationProperties注解
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | package com.test.controller.model; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component //使用Component注解,声明一个组件,被控制器依赖注入 @ConfigurationProperties (prefix = "obj" ) //obj为配置文件中key的前缀 public class StudentProperties { private String ssname; private int ssage; public String getSname() { return ssname; } public void setSname(String sname) { this .ssname = sname; } public int getSage() { return ssage; } public void setSage( int sage) { this .ssage = sage; } @Override public String toString() { return "StudentProperties [ssname=" + ssname + ", ssage=" + ssage + "]" ; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | package com.test.controller; import com.test.controller.model.StudentProperties; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.env.Environment; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class StartMainController { @Autowired StudentProperties studentProperties; @Autowired private Environment ev; @Value ( "key_test_001" ) private String as; @RequestMapping ( "/12" ) @ResponseBody public String home12() { return "hello---12" ; } @RequestMapping ( "/123" ) @ResponseBody public String home123() { return "hello---123" ; } @RequestMapping ( "/1234" ) @ResponseBody public String home1234() { return ev.getProperty( "key_test" ); } @RequestMapping ( "/12345" ) @ResponseBody public String home12345() { return as; } @RequestMapping ( "/123456" ) @ResponseBody public String home123456() { return studentProperties.toString(); } } |
======================================================================================================
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架