美文网首页
Spring Boot笔记(1)Spring Boot入门——更

Spring Boot笔记(1)Spring Boot入门——更

作者: 冷寒 | 来源:发表于2021-02-24 01:02 被阅读0次

一、Spring Boot 入门

1、Spring Boot 简介

2、微服务

3、环境准备

1、MAVEN设置

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

<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 http://maven.apache.org/xsd/settings-1.0.0.xsd">
  <!-- localRepository
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ${user.home}/.m2/repository
  <localRepository>/path/to/local/repo</localRepository>
  -->
  <localRepository>/Volumes/storage/maven_repository</localRepository>

  <!-- Apache Maven 配置 -->
  <pluginGroups/>
  <proxies/>
  <!-- 阿里云镜像 -->
  <mirrors>
  <!--<mirror>
        <id>aliyunmaven</id>
        <mirrorOf>*</mirrorOf>
        <name>阿里云公共仓库</name>
        <url>https://maven.aliyun.com/repository/public</url>
      </mirror>-->

      <mirror>
          <!--This sends everything else to /public -->
          <id>nexus-aliyun</id>
          <mirrorOf>*</mirrorOf>
          <name>Nexus aliyun</name>
          <url>http://maven.aliyun.com/nexus/content/groups/public</url>
      </mirror>
  </mirrors>
    
      

  <!-- 配置: java8, 先从阿里云下载, 没有再去私服下载  -->
  <!-- 20190929 hepengju 测试结果: 影响下载顺序的是profiles标签的配置顺序(后面配置的ali仓库先下载), 而不是activeProfiles的顺序 -->
  <profiles>
      <!-- 全局JDK1.8配置 -->
      <profile>
          <id>jdk1.8</id>
          <activation>
              <activeByDefault>true</activeByDefault>
              <jdk>1.8</jdk>
          </activation>
          <properties>
              <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
              <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>
      <!-- Nexus私服配置: 第三方jar包下载, 比如oracle的jdbc驱动等 -->
      <profile>
          <id>dev</id>
          <repositories>
              <repository>
                  <id>nexus</id>
                  <url>http://nexus.hepengju.cn:8081/nexus/content/groups/public/</url>
                  <releases>
                      <enabled>true</enabled>
                  </releases>
                  <snapshots>
                      <enabled>true</enabled>
                  </snapshots>
              </repository>
          </repositories>
          <pluginRepositories>
              <pluginRepository>
                  <id>public</id>
                  <name>Public Repositories</name>
                  <url>http://nexus.hepengju.cn:8081/nexus/content/groups/public/</url>
              </pluginRepository>
          </pluginRepositories>
      </profile>
      <!-- 阿里云配置: 提高国内的jar包下载速度 -->
      <profile>
          <id>ali</id>
          <repositories>
              <repository>
                  <id>alimaven</id>
                  <name>aliyun maven</name>
                  <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
                  <releases>
                      <enabled>true</enabled>
                  </releases>
                  <snapshots>
                      <enabled>true</enabled>
                  </snapshots>
              </repository>
          </repositories>
          <pluginRepositories>
              <pluginRepository>
                  <id>alimaven</id>
                  <name>aliyun maven</name>
                  <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
              </pluginRepository>
          </pluginRepositories>
      </profile>
  </profiles>

  <!-- 激活配置 --> 
  <activeProfiles>
      <activeProfile>jdk1.8</activeProfile>
      <activeProfile>dev</activeProfile>
      <activeProfile>ali</activeProfile>
  </activeProfiles>
</settings>

2、IDEA设置

设置JDK以及MAVEN关联

4、Spring Boot Hello World

1、创建一个MAVEN工程

2、导入依赖Spring Boot 相关的依赖

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.2.RELEASE</version>
    <relativePath/>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

3、编写一个主程序:启动Spring Boot应用

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

4、编写相关的Controller、Service

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

5、运行测试

<!--这个插件可以将应用打包成一个可执行的jar-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

将应用package打包成jar包,直接使用java -jar命令运行

P6 00:00

代码地址

相关文章

网友评论

      本文标题:Spring Boot笔记(1)Spring Boot入门——更

      本文链接:https://www.haomeiwen.com/subject/uvrjfltx.html