SSM环境搭建(五) - 使用Spring Initializr搭建Spring Boot Web工程

一.搭建Web工程

万事开头难,自己搭建Web项目结构费时费力,Spring提供了Spring Initializr,它可以为你生成一个基本的Spring Boot项目结构
Spring Initializr官网:https://start.spring.io/

1.生成项目结构

用浏览器打开https://start.spring.io/
这里写图片描述

2.解压到工作空间

我自己空间为:D:\eclipse\workspace\boot\demo
这里写图片描述

3.Eclipse导入项目

File -> Import -> Existing Maven Projects
这里写图片描述

导入成功后:
这里写图片描述

4.集成MVC框架

Spring Boot中集成并配置MVC框架,只需要在pom.xml文件中添加:

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

完整POM文件:

<?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.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.M7</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

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

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

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

    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>


</project>

5.一个简单实例

实例代码:

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class DemoApplication {

    @RequestMapping("/test")
    public String test() {
        return "Hello Spring Boot";
    }

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}
  1. @SpringBootApplication标注DemoApplication 是Spring Boot应用的入口
  2. main方法在应用启动的时候执行
  3. @RestController标注DemoApplication是一个控制器,返回格式为JSON
  4. @RequestMapping来映射URL

6.启动服务

在DemoApplication上Run As -> Java application
Spring Boot内置Tomcat,默认端口号为8080
这里写图片描述

7.访问

浏览器输入:http://localhost:8080/test
这里写图片描述

二.Web应用目录结构

Spring Boot应用中资源存放在一些默认路径下:

  1. Web模板文件存放在resources/templates目录下
  2. 模板文件使用的静态资源存放在resources/static目录下
  3. 在resources目录下application.properties或application.yml,这个文件会被发布到classpath中,Spring Boot会自动读取
  4. 程序入口类DemoApplication应该放在根路径下,这样才会扫描自动加载DemoApplication所处包下及其子包中符合条件的bean

这里写图片描述

三.增加Web支持

以前根据Spring创建一个Web应用,需要配置web.xml、applicationContext.xml,使用Spring Boot,仅仅需要在pom.xml中配置Spring Boot,并添加spring-boot-starter-web的依赖即可:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.M7</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

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

四.使用热部署

参考:Spring Boot干货系列:(十)开发常用的热部署方式汇总
上面的例子,修改类时必须重新启动应用,对于开发者非常不方便,Spring Boot提供了spring-boot-devtools,它能在修改类或配置文件时自动重新加载Spring Boot应用
需要在pom.xml中添加以下依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
</dependency>
1.第一次启动
Registering beans for JMX exposure on startup
o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8090 (http) with context path ''
com.example.demo.DemoApplication         : Started DemoApplication in 6.663 seconds (JVM running for 7.502)
2.修改类
LiveReload server is running on port 35729
Registering beans for JMX exposure on startup
Tomcat started on port(s): 8090 (http) with context path ''
Condition evaluation unchanged
Started DemoApplication in 2.664 seconds (JVM running for 29.787)

主要多了2个变化:

  1. LiveReload server用于监控Spring Boot应用文件的变化
  2. 启动速度快。Spring Boot再次重启避免重启了Tomcat,也避免重启已经加载的Spring相关类,只重新加载变化的类,所以速度很快

五.查看已启用的自动配置

可以通过下面三种方式查看当前项目中已启用和未启用的自动配置的报告:

  • 运行jar时增加–debug参数:
java -jar xx.jar --debug
  • 在application.properties中设置属性
debug=true
  • 在STS中设置
    这里写图片描述

此时启动,可在控制台输出,已启用的自动配置:

Positive matches:
-----------------

   CodecsAutoConfiguration matched:
      - @ConditionalOnClass found required class 'org.springframework.http.codec.CodecConfigurer'; @ConditionalOnMissingClass did not find unwanted class (OnClassCondition)

   CodecsAutoConfiguration.JacksonCodecConfiguration matched:
      - @ConditionalOnClass found required class 'com.fasterxml.jackson.databind.ObjectMapper'; @ConditionalOnMissingClass did not find unwanted class (OnClassCondition)

   CodecsAutoConfiguration.JacksonCodecConfiguration#jacksonCodecCustomizer matched:
      - @ConditionalOnBean (types: com.fasterxml.jackson.databind.ObjectMapper; SearchStrategy: all) found bean 'jacksonObjectMapper' (OnBeanCondition)

   DispatcherServletAutoConfiguration matched:
      - @ConditionalOnClass found required class 'org.springframework.web.servlet.DispatcherServlet'; @ConditionalOnMissingClass did not find unwanted class (OnClassCondition)
      - found ConfigurableWebEnvironment (OnWebApplicationCondition)
...

未启动的自动配置:

Negative matches:
-----------------

   ActiveMQAutoConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required classes 'javax.jms.ConnectionFactory', 'org.apache.activemq.ActiveMQConnectionFactory' (OnClassCondition)

   AopAutoConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required classes 'org.aspectj.lang.annotation.Aspect', 'org.aspectj.lang.reflect.Advice', 'org.aspectj.weaver.AnnotatedElement' (OnClassCondition)

   ArtemisAutoConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required classes 'javax.jms.ConnectionFactory', 'org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory' (OnClassCondition)

   BatchAutoConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required classes 'org.springframework.batch.core.launch.JobLauncher', 'org.springframework.jdbc.core.JdbcOperations' (OnClassCondition)
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值