SpringBoot项目中的 ClassPath路径指的是哪个路径?

针对SpringBoot项目来说,ClassPath指的是哪些路径?

一直对classpath这个概念理解得不是很清晰,今天在网上看了博客专门研究了一下,做一点记录。

  1. 首先,对于SpringBoot项目来说,classpath指的是src.main.java和src.main.resources路径以及第三方jar包的根路径,存放在这两个路径下的文件,都可以通过classpath作为相对路径来引用;
    红框中的就是classpath

  2. src.main.java和src.main.resources路径下的内容,在执行了Maven的Compile后,都会被放到target.classes目录下,包括.class文件,静态资源文件(图片,CSS,jQuery文件等);

  3. **对于第三方jar包,如何体现其也是classpath路径之一?**执行Maven的package生命周期后,将得到的jar包解压,得到以下目录
    在这里插入图片描述
    在BOOT-INF文件夹下,有以下内容:
    在这里插入图片描述
    分别打开来看看:

  4. 首先是classes文件夹,内容如下图。classes文件夹中包含的是src/main/java和src/main/resources这两个路径下的内容(由于src/main/resources/static文件夹和src/main/resources/template文件夹是空的,所以没有打包进来)。
    在这里插入图片描述

  5. 再是lib文件夹,内容如下图。里面是项目依赖的第三方jar包
    在这里插入图片描述

  6. 然后是classpath.idx文件。从文件名可知这个文件与classpath有关,打开之后的内容如下。这是项目所引入的jar包的报名(也可以理解为根路径)。猜测SpringBoot会根据这个文件将jar包根路径包含到classpath中。
    在这里插入图片描述

总结

由上可知,SpringBoot项目的classpath包含三个:

  1. src/main/java路径
  2. src/main/resouces路径
  3. 第三方jar包的根路径
    在这三个路径下的文件,都可以认为是放在了classpath路径下的,可以使用classpath关键字作为相对路径的来引用。不过这些并没有得到验证,仅仅是我根据网上的博客做的总结。

参考:https://www.cnblogs.com/eden-libinglin/p/13831822.html

  • 40
    点赞
  • 75
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
适合初学 springboot 的同学 --------------------------- maven配置:pom.xml --------------------------- <?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> --------------------------- src/main/resources/application.yml --------------------------- spring: # 定静态资源的路径 resources: static-locations: classpath:/static/,classpath:/views/ thymeleaf: prefix: classpath:/templates/ server: port:8080 context-path:"/" --------------------------- DemoApplication 运行 main 方法即可启动 springboot --------------------------- package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; @SpringBootApplication @ComponentScan(basePackages = {"com.example.*"}) //定扫描包路径 public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } --------------------------- HelloWorldController --------------------------- package com.example.controller; import java.util.HashMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; // @RestController返回的是json @RestController public class HelloWorldController { // http://localhost:8080/hello 返回的是文本"Hello World" @RequestMapping("/hello") public String index() { return "Hello World"; } /** * 本地访问内容地址 :http://localhost:8080/hello3 ;返回的是文本 */ @RequestMapping("/hello3") public String helloHtml(HashMap<String, Object> map) { map.put("hello", "欢迎进入HTML页面"); return "/index"; } } --------------------------- HelloWorldController --------------------------- package com.example.controller; import java.util.HashMap; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class SpringBootController { /** * 本地访问内容地址 :http://localhost:8080/hello2 ; 可访问到 * src/main/resources/views/index.html */ @RequestMapping("/hello2") public String helloHtml(HashMap<String, Object> map) { // 传参数hello到html页面 map.put("hello", "欢迎进入HTML页面"); return "/index"; } } --------------------------- src/main/resources/views/index.html --------------------------- <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> aaaaaaaaaaaaaaaaaaaaaaacccccccccccccc <p th:text="${hello}">dddd</p> </body> </html> --------------------------- 直接访问静态页面 --------------------------- http://localhost:8080/index.html 可直接访问到 src/main/resources/templates/index.html

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值