file:${user.dir}:spring读取工程外的properties文件

本文介绍了如何在Spring中配置properties文件的位置,以便于在不同环境中(如Linux和Windows)进行灵活配置。通过使用${user.dir}

因为生产和开发测试的环境不同,所以有时候需要把properties文件放在包外方便修改配置。 

 spring配置文件如下:

 

     <context:property-placeholder location="file:${user.dir}/wbaobei/conf/data-access.properties"/>


linux环境:file:${user.dir}代表的位置是tomcat目录bin下。(由于项目是在bin的上一级启动的,所以这代表的是和bin同一级的目录)

 

windows环境:file:${user.dir}代表的位置是工程根目录下。

 

 

Microsoft Windows [版本 10.0.19045.3448] (c) Microsoft Corporation。保留所有权利。 D:\nacos-server-2.0.3\nacos\bin>startup.cmd -m standalone "nacos is starting with standalone" ,--. ,--.'| ,--,: : | Nacos 2.0.3 ,`--.'`| ' : ,---. Running in stand alone mode, All function modules | : : | | ' ,'\ .--.--. Port: 8848 : | \ | : ,--.--. ,---. / / | / / ' Pid: 5576 | : ' '; | / \ / \. ; ,. :| : /`./ Console: http://10.30.52.56:8848/nacos/index.html ' ' ;. ;.--. .-. | / / '' | |: :| : ;_ | | | \ | \__\/: . .. ' / ' | .; : \ \ `. https://nacos.io ' : | ; .' ," .--.; |' ; :__| : | `----. \ | | '`--' / / ,. |' | '.'|\ \ / / /`--' / ' : | ; : .' \ : : `----' '--'. / ; |.' | , .-./\ \ / `--'---' '---' `--`---' `----' 2025-09-11 16:01:22,062 INFO Bean 'org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler@6594402a' of type [org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 2025-09-11 16:01:22,068 INFO Bean 'methodSecurityMetadataSource' of type [org.springframework.security.access.method.DelegatingMethodSecurityMetadataSource] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 2025-09-11 16:01:22,405 WARN Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is java.lang.IllegalStateException: Unable to create the directory [D:\nacos-server-2.0.3\nacos\bin\file:.] to use as the base directory 2025-09-11 16:01:22,408 INFO Nacos Log files: D:\nacos-server-2.0.3\nacos\logs 2025-09-11 16:01:22,409 INFO Nacos Log files: D:\nacos-server-2.0.3\nacos\conf 2025-09-11 16:01:22,409 INFO Nacos Log files: D:\nacos-server-2.0.3\nacos\data 2025-09-11 16:01:22,411 ERROR Startup errors : org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is java.lang.IllegalStateException: Unable to create the directory [D:\nacos-server-2.0.3\nacos\bin\file:.] to use as the base directory at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:156) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:544) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:744) at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:391) at org.springframework.boot.SpringApplication.run(SpringApplication.java:312) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1204) at com.alibaba.nacos.Nacos.main(Nacos.java:35) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49) at org.springframework.boot.loader.Launcher.launch(Launcher.java:108) at org.springframework.boot.loader.Launcher.launch(Launcher.java:58) at org.springframework.boot.loader.PropertiesLauncher.main(PropertiesLauncher.java:467) Caused by: java.lang.IllegalStateException: Unable to create the directory [D:\nacos-server-2.0.3\nacos\bin\file:.] to use as the base directory at org.apache.catalina.startup.Tomcat.initBaseDir(Tomcat.java:873) at org.apache.catalina.startup.Tomcat.getServer(Tomcat.java:657) at org.apache.catalina.startup.Tomcat.getService(Tomcat.java:589) at org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getWebServer(TomcatServletWebServerFactory.java:171) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:180) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:153) ... 16 common frames omitted 2025-09-11 16:01:22,412 WARN [WatchFileCenter] start close 2025-09-11 16:01:22,413 WARN [WatchFileCenter] start to shutdown this watcher which is watch : D:\nacos-server-2.0.3\nacos\conf 2025-09-11 16:01:22,413 WARN [WatchFileCenter] already closed 2025-09-11 16:01:22,413 WARN [NotifyCenter] Start destroying Publisher 2025-09-11 16:01:22,414 WARN [NotifyCenter] Destruction of the end 2025-09-11 16:01:22,414 ERROR Nacos failed to start, please see D:\nacos-server-2.0.3\nacos\logs\nacos.log for more details. 2025-09-11 16:01:22,423 INFO Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2025-09-11 16:01:22,427 ERROR Application run failed org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is java.lang.IllegalStateException: Unable to create the directory [D:\nacos-server-2.0.3\nacos\bin\file:.] to use as the base directory at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:156) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:544) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:744) at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:391) at org.springframework.boot.SpringApplication.run(SpringApplication.java:312) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1204) at com.alibaba.nacos.Nacos.main(Nacos.java:35) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49) at org.springframework.boot.loader.Launcher.launch(Launcher.java:108) at org.springframework.boot.loader.Launcher.launch(Launcher.java:58) at org.springframework.boot.loader.PropertiesLauncher.main(PropertiesLauncher.java:467) Caused by: java.lang.IllegalStateException: Unable to create the directory [D:\nacos-server-2.0.3\nacos\bin\file:.] to use as the base directory at org.apache.catalina.startup.Tomcat.initBaseDir(Tomcat.java:873) at org.apache.catalina.startup.Tomcat.getServer(Tomcat.java:657) at org.apache.catalina.startup.Tomcat.getService(Tomcat.java:589) at org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getWebServer(TomcatServletWebServerFactory.java:171) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:180) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:153) ... 16 common frames omitted 分析报错信息
最新发布
09-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值