今天部署windows上的项目时遇到了个问题:在IDEA中能正常启动,但是只要用java -jar就没法读取nacos配置。
报错信息如下:总之就是读取不到nacos中的配置
2023-03-19 14:51:11,633 [main] WARN [io.undertow.websockets.jsr] Bootstrap.java:68 - UT026010: Buffer pool was not set on WebSocketDeploymentInfo, the default pool will be used
2023-03-19 14:51:11,656 [main] INFO [io.undertow.servlet] ServletContextImpl.java:382 - Initializing Spring embedded WebApplicationContext
2023-03-19 14:51:11,657 [main] INFO [o.s.b.w.s.c.ServletWebServerApplicationContext] ServletWebServerApplicationContext.java:290 - Root WebApplicationContext: initialization completed in 1291 ms
2023-03-19 14:51:11,865 [main] WARN [o.s.b.w.s.c.AnnotationConfigServletWebServerApplicationContext] AbstractApplicationContext.java:591 - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name '*****Controller': Unsatisfied dependency expressed through field '*****Service'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'iceDynamicsServiceImpl': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'ice.iceModelFile.modelDirPath' in value "${ice.iceModelFile.modelDirPath}"
2023-03-19 14:51:11,880 [main] INFO [o.s.b.a.l.ConditionEvaluationReportLoggingListener] ConditionEvaluationReportLoggingListener.java:136 -
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2023-03-19 14:51:11,902 [main] ERROR [org.springframework.boot.SpringApplication] SpringApplication.java:835 - Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'iceDynamicsController': Unsatisfied dependency expressed through field '****Service'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '*****ServiceImpl': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder '***.***.***' in value "${***.***.***}"
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:659)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:639)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1431)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:619)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:953)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:745)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:420)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:307)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1317)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306)
at cn.*****.*****.model.FlModelApplication.main(FlModelApplication.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
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.JarLauncher.main(JarLauncher.java:88)
解决方式:启动时使用:java -jar -Dfile.encoding=UTF-8 ****.jar
原因:可能是由于当您运行 java -jar 命令时,JVM使用了不同的默认编码(默认情况下是操作系统默认编码)。这可能会导致应用程序无法正确地读取和处理配置信息等内容。
添加 -Dfile.encoding=UTF-8 参数是一种常见的解决方法,因为它将强制JVM使用UTF-8编码。这确保了应用程序可以正确地读取和处理文本数据。