SpringBoot快速上手

1.Maven

参考链接

下载Maven

http://maven.apache.org/download.cgi

maven配置

下载好maven解压到本地目录

  1. 添加环境变量
  2. 打开conf/setting.xml中配置location标签 填写本地仓库路径
  3. Idea中配置maven

maven如何找jar包的?

通过pom.xml的配置就能够获取jar包。但是jar在哪里? 仓库

  • 本地仓库 maven会将工程中需要的jar包从远程下载到本地的一个目录(默认user.home/.m2/repository)
  • 第三方仓库也成为私服。
  • 中央仓库。(默认http://repo1.maven.org/maven2)

获取流程先在本地仓库查找没有就去私服私服也没有就到中央仓库下载,下次试用就是本地仓库了

2.使用Maven构建项目

打开IDEA配置maven。

步骤

  1. Idea中创建一个Maven项目
  2. 引入springBoot依赖
  3. 添加SpringBoot启动器依赖
  4. 编写启动类
  5. 编写Controller直接访问
  6. SpringBoot热部署

通过创建Maven项目

  • scr/main/java程序入口
  • src/main/resources配置文件
    • static 静态资源文件
    • templates 模板文件
    • application.properties 配置文件
  • src/test测试入口

添加依赖

<?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.itaolaity</groupId>
    <artifactId>SpringBoot1</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!--引入springboot依赖-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.0.RELEASE</version>
    </parent>

    <!--添加启动器依赖-->
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <!--引入web模块-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

编写启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;

@SpringBootApplication
public class Application implements EmbeddedServletContainerCustomizer{
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
    public void customize(ConfigurableEmbeddedServletContainer container) {
        // TODO 自动生成的方法存根
        container.setPort(8088);
    }
}

打开浏览器

http://127.0.0.1:8088/

HelloWorld

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;



@Controller
public class HelloWorld {
    @ResponseBody
    @RequestMapping("helloworld")
    public String helloworld(){
        return "Hello world";
    }
}
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


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

就这样无需创建jsp页面,页面也有Hello word 神奇!

SpringBoot内置了tomcat,并且不需要达成war执行可以在application.properties对端口号进行修改server.port=8888

3.IDEA创建maven web项目

  1. file->new->project
  2. 新建webapp

  1. main目录新建java目录resources目录

    1. java标记 sources Root
    2. resources标记Resources Root
  2. Project Structure配置

  1. Tomcat配置

参考连接

配置

@SpringBootApplication

SpringBoot的主配置类

  • @SpringBootConfiguration 包含@Configuratuon表示 “配置类”
      1. 该类是一个配置类
      1. 该类自动加入spring容器

@EnableAutoConfiguration

使SpringBoot自动配置,可以找到@SpringBootApplication所在类的包以及子包全部放入Spring容器


  转载请注明: linis SpringBoot快速上手

  目录