The user-defined configuration file is placed in the Resources folder, read by org.springframework.core.io.urlresource, and the path is in classpath: mode. There is no problem with ordinary spring projects. It’s OK to use the war package for spring boot, but when you start the application, you throw an exception: java.net.malformedurlexception: unknown protocol: classpath. This startup method does not support classpath: this resource location, so we find a new way to replace it: org. Springframework. Util. Resourceutils
Old code:
UrlResource urlResource = new UrlResource("classpath:xxx/xxx.properties");
Properties prop = new Properties();
prop.load(urlResource.getInputStream());
//Omit the part about getting the prop property
New code:
File file = ResourceUtils.getFile("classpath:xxx/xxx.properties");
Properties prop = new Properties();
InputStream is = new FileInputStream(file);
//Omit the part about getting the prop property
prop.load(is);
is.close();
Perfect solution
Similar Posts:
- [Solved] Access denied for user ‘Administrator’@’localhost’ (using password: YES)
- How hibernate 3 used in jbpm4.4 is compatible with spring 5.X and the exception caused by: java.lang.classnotfoundexception: org.hibernate.impl.sessionimpl
- Cannot find the declaration of element ‘beans’
- [Solved] Swagger Error: Failed to start bean ‘documentationPluginsBootstrapper’
- [Solved] Spring introduces the properties variable Error: org.springframework.beans.TypeMismatchException…
- [transfer] could not resolve placeholder solution
- [Solved] Context initialization failed: org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name ‘dataSource’ defined in URL…
- [Solved] Context initialization failed: org.springframework.beans.factory.BeanDefinitionStoreException
- An error occurs when springboot sets the log level [How to Solve]
- [Solved] Project Start Error: Instantiation of bean failed; nested exception is java.lang.ExceptionInInitializerError