Spring Boot触发完全重启,而不是重新加载静态文件的更改 [英] Spring Boot triggers full restart instead of reload on changes over static files

查看:145
本文介绍了Spring Boot触发完全重启,而不是重新加载静态文件的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Spring Boot项目中,我具有以下结构:

In my Spring Boot project I have the following structure:

- src
  - main
    - java
    - resources
      - static
        - css
        - js
        - img
    - webapp
      - WEB-INF
          - views

按照

As per the documentation, resources inside static/ should trigger a reload, but instead whenever I save a file inside css/ or js/ a full restart is triggered.

我正在使用Spring Boot 2.0.4,其中包括spring-boot-devtools(默认配置)以及带有Eclipse Oxygen和Tomcat 8.5的spring-boot-starter-security

I'm using Spring Boot 2.0.4 including spring-boot-devtools (default configuration) and spring-boot-starter-security with Eclipse Oxygen and Tomcat 8.5

包装类型为 war ,因为我必须部署到共享容器,并且视图是使用 jsp 创建的.

Packaging type is war since I have to deploy to a shared container and views are created using jsp.

任何帮助将不胜感激.

相关文件:

pom.xml:

<packaging>war</packaging>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.4.RELEASE</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-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>com.microsoft.sqlserver</groupId>
        <artifactId>mssql-jdbc</artifactId>
        <scope>runtime</scope>
    </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.springframework.security</groupId>
        <artifactId>spring-security-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-envers</artifactId>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.2.1-b03</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
    </dependency>
</dependencies>

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

application.properties:

application.properties:

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

#spring.jpa.show-sql=true

spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.SQLServer2008Dialect

spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

spring.jpa.open-in-view=false

推荐答案

最终找到了解决方案.这比我想象的要简单得多.

Finally found the solution. It was much simpler than I thought.

由于我使用战争包装,因此可以使用webapp的默认行为.源文件夹下的所有内容都会触发重新启动,但是webapp下的资源会在运行时获取,因此无需重新加载任何内容.

Since I'm using war packaging I can use webapp's default behaviour. Everything under source folders trigger a restart, but resources under webapp are fetched at runtime, so there is no need to reload anything.

我只是将静态内容放在/webapp/static下,然后添加了资源处理程序:

I just placed my static content under /webapp/static and then added the resource handler:

@Configuration
public class MvcConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("/static/"); 
    }
}

只需对静态变量(基本上是资源下的任何内容)都不使用默认的Spring Boot放置,就可以了.

Just don't use the default Spring Boot placement for statics (basically anything under resources) and you'll be fine.

这篇关于Spring Boot触发完全重启,而不是重新加载静态文件的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆