SpringBoot学习笔记-Chapter2(hello word)

开篇

第一次在博客园上写博客,初衷是想记录一下学习笔记,以往都是用笔去记录下学习笔记,现在来看在效率、检索速度上以及可可复制性都不好。作为一名Java开发人员 不会Spring Boot一定会被鄙视的,赶紧追赶时代的脚步,业余时间学习一下SpringBoot,这篇就是跟着文档做一个HelloWord

SpringBoot--HelloWord

首先搭建一个maven的空项目,这个步骤就不细说了。

1、编写pom.xml文件 引入springBoot:

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.dzh</groupId>
    <artifactId>chapter2</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>Chapter2</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <scope>provided</scope>
        </dependency>

    </dependencies>

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

</project>

 2、创建SpringBootServletInitializer的实现类:

package com.dzh.chapter2.main;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Chapter2Application.class);
    }

}

3、编写一个启动类:

package com.dzh.chapter2.main;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Chapter2Application {

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

}

  代码中的@SpringBootApplication这个注解代表着这是一个springBoot的入门文件

4、创建配置文件(不需要配置时,可以省略这个配置文件)application . properties :

springBoot内置tomcat等容器,当本demo启动时,会默认 启动的8080端口(tomcat),想要修改端口信息,可以在application.properties文件中添加一行:

server.port=8090

以Java Application的方式启动main方法就可以看到8090端口的tomcat启动成功了。

spring Boot秉承着“约定大于配置”的理念,他对大部分的配置做了一个默认值,比如本项目,启动后会默认使用tomcat并默认8080端口,当你有不同的要求时,你可以在配置中声明。

Spring Boot的参数除了使用properties文件之外,还可以使用yml文件等,它会以下列的优先级顺序进行加载:

  • 命令行参数
  • 来自java:comp/env的JNDI属性
  • java系统属性(System.getProperties())
  • 操作系统环境变量
  • RandomValuePrope1tySource 配的 random. *属性值
  • jar 包外部的 application- {profi l e} .properties 或 application.yml 带 spring.profile )配置文
  • jar 包内部的 application-{profile} .properties 或 application.ym (带 spring.profile )配置文件
  • jar 包外部的 application.properties 或 application .yml (不带 spring.profile )配置文件
  • jar 包内部的 application .properties application .ym (不带 spring.profile )配置文件
  • @Configuration 注解类上的@PropertySource
  • 通过 SpringApplication .setDefaultProperties 指定的默认属性

下面来做一个简易页面,并访问这个页面

在配置文件中声明一下,视图解析器的前缀和后缀:

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

 创建一个controller:

 1 package com.dzh.chapter2.main;
 2 
 3 import org.springframework.stereotype.Controller;
 4 import org.springframework.web.bind.annotation.RequestMapping;
 5 import org.springframework.web.bind.annotation.ResponseBody;
 6 
 7 /**
 8  * @Description TODO
 9  * @ClassName IndexController
10  * @Author DingZuoHeng
11  * @Date 2019/5/8
12  * @Version 1.0
13  **/
14 @Controller
15 public class IndexController {
16 
17     @RequestMapping("/index")
18     public String index(){
19         return "index";
20     }
21 }

在webapp/WEB-INF/下创建jsp目录,并在jsp目录下创建index.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Spring boot 视图解析器</title>
</head>
<body>
<h1>测试视图解析器</h1>
</body>
</html>

 这时就完成了,启动main方法,在浏览器中访问localhost:8090/index

猜你喜欢

转载自www.cnblogs.com/dingzuoheng/p/10843924.html
今日推荐