获取springboot的配置文件内容(springboot获取properties中的内容)

本文主要介绍获取springboot的配置文件内容(springboot获取properties中的内容),下面一起看看获取springboot的配置文件内容(springboot获取properties中的内容)相关资讯。
springboot可以通过@profile在配置类中定义配置环境( 发展与创新),在实际开发中可能需要方法级的控制,也就是说可以获取当前环境的具体信息。
最后,通过查看springboot日志和源代码找到答案。
springboot的启动日志里有以下句子:15 : 57 : 56.128【重启main】。以下配置文件是activ:开发跟踪代码:springapplication.run方法public configuration application context。xt运行(字符串...args) { stopwatch秒表=新秒表;秒表. start;configurableapplicationcontext上下文= nullfailureanalyzers analyzers = nullconfigureheadlessproperty;springapplicationrunlisteners listeners = getrunlisteners(args);listeners . starting;try { application arguments application arguments = new defaultapplicationarguments(args);configurable environment environment = prepare environment(listeners,application arguments);banner printed banner = print banner(环境);context = createapplicationcontext;分析器=新的故障分析器(上下文);准备上下文(上下文t、环境、侦听器、applicationarguments、printed banner);//此处打印,后面是refreshcontext(上下文);afterrefresh(上下文,应用程序参数);listeners.finished(上下文,空);秒表. stop;if(this . logstartupinfo){ new startupinfologger(this . mainapplicationclass)。logstarted(getapplicationlog,秒表);}返回上下文;} catch(throwable ex){ handlerunfailure(context,listeners,analyzers,ex);抛出新的illegalstateexception(ex);}}跟踪代码:spring应用程序。preparecontext方法private void preparecontext(可配置环境上下文、springapplicationrunlisteners侦听器、application arguments application arguments、banner printed banner){ context . set environment(环境);postprocessapplicationcontext(上下文);applyinitializers(上下文);listeners.contextprepared(上下文);if(this . logstartupinfo){ logstartupinfo(context . get parent= = null);logstartupprofileinfo(上下文);//名字显而易见,继续追踪在}...}跟踪代码:springapplica。tion.logstartupprofileinfo的方法protected void logstartupprofileinfo(configurationapplicationcontext context){ log log = getapplicationlog;if(log . isinfoenabled){ string[]active profiles = context . get environment。getactiveprofiles;if(object utils . isempty(active profiles)){ string[]default profiles = cont: string utils . arraytocommadelimitedstring(default profiles));} else { log . info( 以下简介是activ: string utils . arraytocommadelimitedstring(active profiles));//我找到了。显然,我使用了applicationcontxt容器。下一步是编写工具类来获取应用程序。}}}编写springcontextholder工具类@ slf4jpublic类springcontextholder实现applicationcontextaware,disposable bean { private static application context application context = null;/* * *获取静态变量中存储的applicationcontext。*/public static application context getapplicationcontext{ assertcontextobject;退货申请icationcontext}/* * *从静态变量applicationcontext中获取bean,并自动将其转换为所分配对象的类型。*/public static t t get bean(字符串名){ assertcontextinjected;return(t)application context . get bean(name);}/* * *从静态变量applicationcontext中获取bean,并自动将其转换为所分配对象的类型。*/public static t t get bean(class required type){ assertcontextinjected;返回application context . get bean(required type);}/* * *检查applicationcontext是否不为空。*/private static void assertcontextobject{ if(application context = = null){ throw new illegalstateexception( applicaisetapplicatpcontextoncontext属性未注入,请在applicationcontext 。或者在springboot启动类中注册springcontextholder。 );}}/* * *获取当前环境* @ return */public static string get active profile{返回应用程序上下文。getenvironment。get active profiles[0];}/* * *将springcontextholder中的applicationcontext清除为null。*/public static voidclearholder{ log . debug( 清除applicationcontext: springcontextholder中的applicationcontext应用程序上下文= null;} @override public void destroy抛出异常{ springcontextholder . clear holder;} @ override public void setapplicationcontext(application context application context)抛出beans exception { if(spring context holder . application context!= null){ log . warn( springcontextholder中的applicationcontext被覆盖,原applicationcontext为: springcontextholder . application上下文);} springcontextholder . application context = application cont测试环境中控制当前访问的界面url的打印。
如果( 发展与创新。equals (springcontextholder。getactiveprofile){ system . out . println(string . format( = = = =当前界面url : % s ,r:
环境变量
了解更多获取springboot的配置文件内容(springboot获取properties中的内容)相关内容请关注本站点。