SpringBoot study notes (II deployment)

Springboot and web applications we learn not the same as before, which is essentially a Java application, and how to deploy it?
Generally speaking, Springboot will be deployed in two ways: all packaged into a jar, or packaged into a war.

A, jar way

1, as already written a spring boot project, ( IJ create a spring boot project ) dos command to enter the project directory
Here Insert Picture Description

2, packaged into jar
input commands

mvn install

Here Insert Picture Description
In the target directory swells into a jar file

3, run the jar

java -jar target/springboot-0.0.1-SNAPSHOT.jar

Here Insert Picture DescriptionSuch is the success of the operation

4, visit http: // localhost: 8080 / hello
Here Insert Picture Description

Two, war way

This stuff is really a pit ah, engage in more than a day, all kinds of errors, people really could not help feeling that he has of his Mother.
So better not to use ij directly create Spring Boot,
Do not be so builtwell into the normal steps of:

1, with the IJ build a maven project

2, the specific code

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>edu.hpu</groupId>
    <artifactId>springboot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot</name>
    <description>springboot</description>
    <packaging>war</packaging>    <!--表示打包成war-->

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
    </parent>

    <dependencies>
        <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>
              <!--打包的时候可以不用包进去,别的设施会提供。事实上该依赖理论上可以参与编译,测          试,运行等周期。
        相当于compile,但是打包阶段做了exclude操作-->
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

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

</project>

That dependence annotate expressed exclude spring boot of the built-in container, spring boot built-in container is tomcat8, deployed to the other version of tomcat will complain, there is another exclusion wording

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

Application.java

package edu.hpu.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.support.SpringBootServletInitializer;//不要导错包了

@SpringBootApplication
@ServletComponentScan   
public class Application extends SpringBootServletInitializer {

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

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

This and previous SpringBoot project has three differences:
(1) inheritance org.springframework.boot.web.servlet.support.SpringBootServletInitializer
(2), add annotations @ServletComponentScan
(3), rewrite configure method

HelloController.java

package edu.hpu.springboot.web;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    @RequestMapping("/hello")
    public String hello(){
        return "你好,好他妈头疼";
    }
}

3、dos窗口进入项目所在目录,输入命令mvn clean package
Here Insert Picture Description接下来查看项目的target目录
Here Insert Picture Description可以看到生成了一个war文件,把文件重命名为ROOT.war,这样做的目的是把它放在webapps目录下时,可以直接通过localhost:8080/hello访问,不然的话这个链接就太长了。
接下来把webapps下的文件都删掉,这是为了防止访问时出现404错误。
Here Insert Picture Descriptionstartup.bat,启动tomcat,一堆可以作为三流科幻片黑客入侵画面的代码闪过
Here Insert Picture Description这样tomcat就跑起来了,访问网址localhost:8080/hello
Here Insert Picture Description这个倒霉催的Spring Boot war部署方式就算是完成了。上面最后生成的war文件也在这了。

ROOT.war

三、热部署

热部署:对于Java应用程序来说,热部署就是在运行时更新Java类文件。使用热部署可以不重启应用的情况下,更新应用。举个例子,就像电脑可以在不重启的情况下,更换U盘。热部署是利用类加载机制实现的,项目更新后,销毁该自定义ClassLoader,更新class类文件,创建新的ClassLoader去加载更新后的class类文件。

SpringBoot热部署:
1、在pom.xml中新增依赖

<!--新增热部署插件-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>         <!--这个设置为true热部署才会生效-->
        </dependency>

完整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>edu.hpu</groupId>
    <artifactId>springboot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot</name>
    <description>springboot</description>
    <packaging>war</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
    </parent>

    <dependencies>
        <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>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <!--新增热部署插件-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>         <!--这个设置为true热部署才会生效-->
        </dependency>
    </dependencies>

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

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

</project>

2、运行Application,访问网址localhost:8080

Here Insert Picture Description
3、修改HelloController内容,重新启动Application,访问网址loclhost:8080
Here Insert Picture Description
Here Insert Picture Description

四、错误及解决

Perhaps you are running on a JRE rather than a JDK
Here Insert Picture Description
literally, probably should be configured with the jdk become jre, jdk want it, then give it right. Checked, this is the old pit was before, when installed before the Java, jdk and jre no points so open, so now sit wax, no way to reconfigure it. Java JDK download and install, and configure the environment
Here Insert Picture Description

Other reconstruction projects in the wake of the pit were all filled up.

参考:
【1】、https://blog.csdn.net/qq_33512843/article/details/80951741
【2】、https://blog.csdn.net/yulutian/article/details/80157626?utm_source=blogxgwz1
【3】、http://how2j.cn/k/springboot/springboot-war/1655.html#nowhere
【4】、https://www.cnblogs.com/pfxiong/p/4070462.html

Guess you like

Origin blog.csdn.net/sinat_40770656/article/details/89343809