1.使用 ResourceLoader:
@Autowired
private ResourceLoader resourceLoader;
public void getClassPath() {
Resource resource = resourceLoader.getResource("classpath:");
try {
String classpath = resource.getURL().getPath();
System.out.println("Classpath: " + classpath);
} catch (IOException e) {
e.printStackTrace();
}
}
2.使用 ClassPathResource:
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public void getClassPath() {
Resource resource = new ClassPathResource("");
try {
String classpath = resource.getURL().getPath();
System.out.println("Classpath: " + classpath);
} catch (IOException e) {
e.printStackTrace();
}
}