编译时src/main/java中*.java文件会被编译成*.class文件,在classpath中创建对应目录及class文件
src/main/resources目录中的文件会被复制到classpath目录中
import java.io.InputStream; import java.net.URL; import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; @Component public class Test implements CommandLineRunner { public void run(String... args) throws Exception { //当前项目根目录 System.out.println(System.getProperty("user.dir")); //classpath目录, 一般是根目录下/target/classes目录 URL url = getClass().getClassLoader().getResource(""); System.out.println(url.getPath()); //读取根目录下/target/classes/test/settings.properties文件,即读取src/main/resources/test/settings.properties文件 InputStream inputStream = getClass().getClassLoader().getResourceAsStream("test/settings.properties"); System.out.println(inputStream); } }