初探SpringBoot

本文详细介绍如何修改Maven仓库地址以加速项目构建,并通过实例演示首个SpringBoot程序的搭建流程,包括端口修改、自动配置及启动器的使用。

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

修改maven地址

修改maven地址 在setting maven中设置,选择overwrite ,去到对应的文件夹下面创建settings文件

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
                          https://maven.apache.org/xsd/settings-1.0.0.xsd">  
<mirrors>
     <mirror>  
        <id>alimaven</id>  
        <name>aliyun maven</name>  
        <url>http://maven.aliyun.com/nexus/content/groups/public/</url>  
        <mirrorOf>central</mirrorOf>          
    </mirror>  
  </mirrors>
</settings>
第一个SpringBoot程序

创建第一个Springboot

在pom.xml中添加

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>

在Application同級目录下创建Controller文件夹创建一个测试类Hello,暂时不管注解的含义

@Controller
@RequestMapping("/hello")
public class HelloController {
    @GetMapping("/hello")
    @ResponseBody
    public String hello(){
        return "hello";
    }
}

重新启动项目,在localhost:8080/hello/hello看到启动成功输出hello

改端口

在resources下的application.properties

server.port=8081

自动配置

pom.xml

  • 标签内点进去有大量的jar包
  • 有大量的启动器
  • 我们写或者引入springBoot依赖的时候,不需要指定版本,因为有版本仓库
启动器
<dependency>
  <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
  • 实际上就是SpringBoot的启动场景;例如

spring-boot-starter-web,他就会帮我们导入web环境的所有依赖

  • SpringBoot将所有的功能场景,变成一个个的启动器

    点开依赖查看jar包,有如下几个我们认识的

<activemq.version>5.15.11</activemq.version>//消息队列
<db2-jdbc.version>11.5.0.0</db2-jdbc.version>//数据库
<ehcache.version>2.10.6</ehcache.version>
<ehcache3.version>3.8.1</ehcache3.version>//缓存

需要声明功能就start启动哪个模块,

进入spring官网https://docs.spring.io/spring-boot/docs/2.2.5.RELEASE/reference/html/using-spring-boot.html#using-boot-starter查看功能对应的启动器

主程序
//标注这个类是一个SpringBoot应用
@SpringBootApplication
public class Class1Application {

    public static void main(String[] args) {
        //将Spring应用启动
        SpringApplication.run(Class1Application.class, args);

    }

}
  • 注解

  • @SpringBootConfiguration
    //SpringBoot的配置
    	@Configuration:spring配置类
    		@Component:这是一个spring组件
    @EnableAutoConfiguration :自动配置
    	@AutoConfigurationPackage:自动配置包
    		@Import({Registrar.class}):导入选择器
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值