SpringBoot初级教程

最近项目中须要使用Spring来开发具备restful风格url的后台应用。因而我就想到了使用SpringBoot进行快速的构建和发布。不浪费时间了,下面就直接切入主题。
主要参考:Spring官网文档java

一、使用IDEA建立Maven项目

输入图片说明

二、修改pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.baron</groupId>
    <artifactId>spring-boot-test</artifactId>
    <version>0.1</version>

    <!-- 指定阿里云的maven仓库,访问国外的实在太慢 -->
    <repositories>
        <repository>
            <id>alimaven</id>
            <name>aliyun maven</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public//</url>
        </repository>
    </repositories>
    
    <!-- 设置pom的父参照 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.2.RELEASE</version>
    </parent>

    <dependencies>
        <!-- Spring Boot 依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <properties>
        <!-- 指定JDK版本 -->
        <java.version>1.8</java.version>
    </properties>
    
    <!-- 指定build 须要使用的maven plugin -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <artifactId>maven-failsafe-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

输入完成以后,右键从新导入maven依赖 输入图片说明web

三、编写代码

Application.java // 用于启动应用spring

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;

// 注解表示了这是一个SpringBoot的Application
// 这个在咱们进行打包的时候会使用到,由于咱们使用spring-boot-maven-plugin打成可执行jar包的时候,
// jar包的Main Class并非咱们的Application,因此加上这个注解便于Spring找到须要启动的应用
// 你也能够测试是否能够在一个jar包内使用SpringBootApplication,从而启动多个SpringBoot应用
@SpringBootApplication
// 虽然@SpringBootApplication注解至关于@Configuration, @EnableAutoConfiguration 和 @ComponentScan
// 可是须要注意,是至关于没有参数的@ComponentScan,没有参数就只会在和Application类的同级目录下寻找
// Spring的Bean,因此咱们这边添加上参数"com.*",就会把com目录下全部的类都扫描一遍
@ComponentScan("com.*")
public class Application {
    public static void main(String[] args) {
        // 启动
        SpringApplication.run(Application.class, args);
    }
    
    // 用于配置SpringBoot启动时候的参数
    // 这个仍是比较有用的
    @Bean
    public EmbeddedServletContainerCustomizer containerCustomizer() {
        return container -> {
            // 配置内部的tomcat容器的监听端口
            container.setPort(80);
            // 配置SpringBoot项目的根路径
            container.setContextPath("/hello");
        };
    }
}

HelloController.java 用于对外提供服务apache

// 此处和Application.java处于不一样的包
package com.baron.controller;

import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

// RestConroller注解的做用就是专门给restful的服务的Controller使用
// 使用此注解解释的类,内部全部提供服务的类的返回值都会被自动解析成json
@RestController
public class HelloController {
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String sayHello() {
        return "Hello, SpringBoot";
    }
}

四、Maven打包成可执行Jar

Tips:此处假设maven安装成功
直接在cmd输入
mvn clean package
便可,会自动把全部的依赖都添加进去。 输入图片说明json

相关文章
相关标签/搜索