Spring Boot UrlResource loading classpath error: unknown protocol: classpath

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: