ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

springBoot2---基础入门

2021-11-04 11:59:40  阅读:180  来源: 互联网

标签:入门 spring boot springframework --- springBoot2 import org public


一、配置

1、maven

conf/setting.xml:选择阿里云快一点

<mirrors>
      <mirror>
        <id>nexus-aliyun</id>
        <mirrorOf>central</mirrorOf>
        <name>Nexus aliyun</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public</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、系统要求

在这里插入图片描述在这里插入图片描述

二、helloWorld

官方文档:https://spring.io/projects/spring-boot#learn

1、创建maven项目

1.1、引入依赖

<!--用于指定其父工程-->
<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>

2、创建主程序

package com.boot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

/*
* @SpringBootApplication:表示这是一个springboot应用
* 也可以有下面这三个注解代替:
* @SpringBootConfiguration
* @EnableAutoConfiguration
* @ComponentScan("com.boot"):括号里面写的是对应的包名
* */
//@SpringBootConfiguration
//@EnableAutoConfiguration
//@ComponentScan("com.boot")
@SpringBootApplication
public class MyApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class,args);
    }
}

3、编写业务

package com.boot.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/*
* @RequestMapping注释提供“路由”信息。它告诉Spring,任何带有/path的HTTP请求都应该映射到home方法。
* @RestController注释告诉Spring将结果字符串直接呈现给调用者。*/
@RestController
public class helloController {

    @RequestMapping("/hello")
    public String hello(){
        return "hello,world.";
    }
}

4、测试

直接运行main方法,
在这里插入图片描述

然后在浏览器中输入:http://localhost:8080/hello

在这里插入图片描述

5、题外话

在springboot中咱们再也不用配置海量的配置文件,仅仅只需要创建一个application.properties文件就够了

需要改什么都可以在里面改

在这里插入图片描述

例如如果要修改端口号的话:

server.port=8888

三、创建可执行的jar

1、添加插件

要创建可执行jar,我们需要将SpringBootMaven插件添加到pom.xml中。要执行此操作,请在dependencies部分下方插入以下行:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

2、打包

在这里插入图片描述

也可以在输入命令:mvn package

成功结果:

在这里插入图片描述

打包的文件会放在target目录下

在这里插入图片描述

在这里插入图片描述

然后再cmd中运行,必须在同级目录下

在这里插入图片描述

3、注意点

取消掉cmd的快速编辑模式:因为程序没运行好时,你点击窗口,程序可能会终止

标签:入门,spring,boot,springframework,---,springBoot2,import,org,public
来源: https://blog.csdn.net/qq_44730336/article/details/121139402

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有