springboot01-HelloWorld


浏览器发送hello请求,服务器接收请求并响应,响应hello world字符串

1.创建一个maven工程(jar)

2.导入springboot相关依赖 + jar包插件

	
        org.springframework.boot
        spring-boot-starter-parent
        2.4.5
         
    


    
        
            org.springframework.boot
            spring-boot-starter-web
        
    


    
    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

3.编写一个主程序

// 标注主程序类,说明这是一个springboot应用
@SpringBootApplication
public class HelloWorldApplication {

    public static void main(String[] args) {
        //spring应用启动起来
        SpringApplication.run(HelloWorldApplication.class,args);
    }
}

4.编写相关controller,service

@Controller
public class HelloController {

    @ResponseBody
    @RequestMapping("/hello")
    public String hello(){
        return "Hello,World";
    }
}

5.运行主程序测试

6.分析

parent父项目用来管理springboot应用里面的所有依赖版本
版本仲裁中心

spring-boot-starter-web

spring-boot-starter: springboot场景启动器:帮我们导入了web模块正常运行所依赖的组件
springboot将所有的功能场景都抽取出来,做成一个个的starters(启动器),只需要在项目里面引入这些starter相关场景的所有依赖都会导入进来
需要什么功能就导入什么场景的启动器

@SpringBootConfiguration

= @Configuration配置类上来标注这个注解(本质是@Component)
+@EnableAutoConfiguration(开启自动配置功能)

@EnableAutoConfiguration = @AutoConfigurationPackage(本质是@Import,给容器中导入一个组件) + @Import(给容器中导入组件)
@AutoConfigurationPackage将主配置类的所在包及下面的左右子包里面的所有组件扫描到Spring容器

7.快速创建一个springboot项目




自动引入的pom文件

<?xml version="1.0" encoding="UTF-8"?>

    4.0.0

    
        org.springframework.boot
        spring-boot-starter-parent
        2.4.5
         
    

    com.kcl
    springboot_helloworld02
    0.0.1-SNAPSHOT
    springboot_helloworld02
    Demo project for Spring Boot
    
        1.8
    


    
        
            org.springframework.boot
            spring-boot-starter-web
        


        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin