SpringBoot学习(第一个Spring Boot)

什么是Spring Boot?

Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。简而言之,Spring Boot通过提供默认配置的方式整合了所有的框架,让我们可以更加简单、快速、方便地构建应用程序。

为什么要用Spring Boot?

Spring Boot包含以下几个特性:

  1. 默认提供了大部分框架的使用方式,方便进行快速集成
  2. Spring Boot应用可以独立运行,符合微服务的开发理念
  3. Spring Boot内置WEB容器,无需部署WAR包即可运行
  4. 提供了各种生产就绪型功能,如指标,健康检查和外部配置
  5. Spring Boot通过网站提供了项目模板,方便项目的初始化
  1. 创建maven工程    和JDKbanben
    maven文件中settings.xml文件设置,配置阿里云镜和JDK版本
      <mirrors>    
        <mirror>
            <id>alimaven</id>
            <mirrorOf>central</mirrorOf>
            <name>aliyun maven</name>
            <url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
        </mirror>
      </mirrors>
    
      <profiles>
        <profile>
          <id>jdk-1.8</id>
          <activation>
    	    <activeByDefault>true</activeByDefault>
            <jdk>1.8</jdk>
          </activation>
          <properties>
            <maven.compiler.source>1.8</maven.compiler.source>
    		<maven.compiler.target>1.8</maven.compiler.target>
    		<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
          </properties>
       </profile>
      </profiles>
  2. 引入依赖
    在pom.xml文件中加入以下依赖
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.5.4</version>
        </parent>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
        </dependencies>
  3. 创建主程序
    @SpringBootApplication 注解:标注这是一个主程序类
    作用:扫描当前包及其子包和标注有@Controller、@Service、@Resposity注解的类,并将其添加到Spring容器进行管理
    
    /**
     * @SpringBootApplication 注解:标注这是一个主程序类
     */
    @SpringBootApplication
    public class ManiApplication {
        public static void main(String[] args) {
    //        启动spring应用
            SpringApplication.run(ManiApplication.class, args);
        }
    }

  4. 编写业务
    @RestController注解,相当于@Controller+@ResponseBody两个注解的结合,返回json数据不需要在方法前面加@ResponseBody注解了,但使用@RestController这个注解,就不能返回jsp,html页面,视图解析器无法解析jsp,html页面
    /**
     * @RestController 注解:相当于下面两个注解
     *      @Controller
     *      @ResponseBody
     */
    @RestController
    public class HelloController {
    
        @RequestMapping("/hello")
        public String handle(){
            return "HelloSpringBoot2";
        }
    }
    
  5. 测试
    直接运行main方法即可
      .   ____          _            __ _ _
     /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
    ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
     \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
      '  |____| .__|_| |_|_| |_\__, | / / / /
     =========|_|==============|___/=/_/_/_/
     :: Spring Boot ::                (v2.5.4)
    
    2021-09-09 22:15:31.185  INFO 12872 --- [           main] com.jxqn.springboot.ManiApplication      : Starting ManiApplication using Java 1.8.0_144 on DESKTOP-C6K2ACT with PID 12872 (E:\Users\ASUS\IdeaProjects\springboot\hellospringboot\target\classes started by ASUS in E:\Users\ASUS\IdeaProjects\springboot)
    2021-09-09 22:15:31.188  INFO 12872 --- [           main] com.jxqn.springboot.ManiApplication      : No active profile set, falling back to default profiles: default
    2021-09-09 22:15:31.932  INFO 12872 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
    2021-09-09 22:15:31.941  INFO 12872 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
    2021-09-09 22:15:31.941  INFO 12872 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.52]
    2021-09-09 22:15:31.944  INFO 12872 --- [           main] o.a.catalina.core.AprLifecycleListener   : An older version [1.2.23] of the Apache Tomcat Native library is installed, while Tomcat recommends a minimum version of [1.2.30]
    2021-09-09 22:15:31.945  INFO 12872 --- [           main] o.a.catalina.core.AprLifecycleListener   : Loaded Apache Tomcat Native library [1.2.23] using APR version [1.7.0].
    2021-09-09 22:15:31.945  INFO 12872 --- [           main] o.a.catalina.core.AprLifecycleListener   : APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true], UDS [false].
    2021-09-09 22:15:31.945  INFO 12872 --- [           main] o.a.catalina.core.AprLifecycleListener   : APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]
    2021-09-09 22:15:31.949  INFO 12872 --- [           main] o.a.catalina.core.AprLifecycleListener   : OpenSSL successfully initialized [OpenSSL 1.1.1c  28 May 2019]
    2021-09-09 22:15:32.044  INFO 12872 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
    2021-09-09 22:15:32.044  INFO 12872 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 809 ms
    2021-09-09 22:15:32.281  INFO 12872 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
    2021-09-09 22:15:32.288  INFO 12872 --- [           main] com.jxqn.springboot.ManiApplication      : Started ManiApplication in 1.455 seconds (JVM running for 2.296)

  6. 简化配置
    #将默认的端口号8080修改为8888
    server.port=8888

                 



2021-09-09 22:17:24.666  INFO 9888 --- [           main] com.jxqn.springboot.ManiApplication      : Starting ManiApplication using Java 1.8.0_144 on DESKTOP-C6K2ACT with PID 9888 (E:\Users\ASUS\IdeaProjects\springboot\hellospringboot\target\classes started by ASUS in E:\Users\ASUS\IdeaProjects\springboot)
2021-09-09 22:17:24.681  INFO 9888 --- [           main] com.jxqn.springboot.ManiApplication      : No active profile set, falling back to default profiles: default
2021-09-09 22:17:25.365  INFO 9888 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8888 (http)
2021-09-09 22:17:25.372  INFO 9888 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-09-09 22:17:25.372  INFO 9888 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.52]
2021-09-09 22:17:25.373  INFO 9888 --- [           main] o.a.catalina.core.AprLifecycleListener   : An older version [1.2.23] of the Apache Tomcat Native library is installed, while Tomcat recommends a minimum version of [1.2.30]
2021-09-09 22:17:25.373  INFO 9888 --- [           main] o.a.catalina.core.AprLifecycleListener   : Loaded Apache Tomcat Native library [1.2.23] using APR version [1.7.0].
2021-09-09 22:17:25.373  INFO 9888 --- [           main] o.a.catalina.core.AprLifecycleListener   : APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true], UDS [false].
2021-09-09 22:17:25.373  INFO 9888 --- [           main] o.a.catalina.core.AprLifecycleListener   : APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]
2021-09-09 22:17:25.376  INFO 9888 --- [           main] o.a.catalina.core.AprLifecycleListener   : OpenSSL successfully initialized [OpenSSL 1.1.1c  28 May 2019]
2021-09-09 22:17:25.440  INFO 9888 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-09-09 22:17:25.440  INFO 9888 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 712 ms
2021-09-09 22:17:25.682  INFO 9888 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8888 (http) with context path ''
2021-09-09 22:17:25.689  INFO 9888 --- [           main] com.jxqn.springboot.ManiApplication      : Started ManiApplication in 1.342 seconds (JVM running for 2.126)

 端口号已经被修改为8888

在浏览器地址栏输入:http://localhost:8888/hello 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值