Spring Boot: 开发web 应用 - 02 探究竟

本文介绍了Spring Boot项目中使用的Starter依赖项,包括spring-boot-starter-web等,并解释了这些依赖如何简化开发流程。同时,文章还展示了如何通过pom.xml文件查看引入的具体jar包。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Look Under The Hood

引入了哪些jar 文件

通过pom.xml 可以看到:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-freemarker</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

暂时我们可以忽略掉spring-boot-starter-freemarker,这个只是引入了对freemarker的支持。

打开Dependency Hierarchy可以看看spring-boot-starter-web帮我们引入了哪些jar。
Dependency Hierarchy

可以从上面看到引入了嵌入式的tomcat;在IDE里面去查询对应的jar文件,spring-boot-starter-* 的jar文件并没有任何的java代码,只是将相关依赖集合起来提供一站式服务。

Starter POMs are a set of convenient dependency descriptors that you can include in your application. You get a one-stop-shop for all the Spring and related technology that you need, without having to hunt through sample code and copy paste loads of dependency descriptors. For example, if you want to get started using Spring and JPA for database access, just include the spring-boot-starter-data-jpa dependency in your project, and you are good to go. Spring Boot Starter POM.

Java 代码

上一节中创建了项目之后,只有两个很简单的Java 类。

SpringbootStaticContentApplication
package com.choelea.springboot.springbootstaticcontent;
.............
@SpringBootApplication
public class SpringbootStaticContentApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootStaticContentApplication.class, args);
    }
}

@SpringBootApplication 只是为了方便集合了Spring Boot 常用的几个注解。(最早的Spring Boot没有这个annotation)
The @SpringBootApplication annotation is equivalent to using @Configuration, @EnableAutoConfiguration and @ComponentScan with their default attributes. Refer to :Using the @SpringBootApplication annotation

@SpringBootApplication 的入口住类最佳实践是放在最顶级的package中;这样默认所有子package中的class都会被Spring扫到。这点可以查看@ComponentScan 的javadoc “If specific packages are not defined, scanning will occur from the package of the class that declares this annotation.

HomePageController

此类只有放在@SpringBootApplication 所在类的下级包内才会被扫描生效。

package com.choelea.springboot.springbootstaticcontent.controller;
.................
@Controller
public class HomePageController {   
    @RequestMapping("/")
    public String home(){
        return "index";
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值