【问题标题】:Spring Boot triggers full restart instead of reload on changes over static filesSpring Boot 触发完全重启而不是重新加载静态文件的更改
【发布时间】:2018-09-04 08:30:43
【问题描述】:

在我的 Spring Boot 项目中,我的结构如下:

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

根据documentation,静态/ 中的资源应该触发重新加载,但是每当我在 css/ 或 js/ 中保存文件时,都会触发完全重启。

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

打包类型是 war,因为我必须部署到共享容器并且使用 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:

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

【问题讨论】:

  • 确保您没有使用以下属性覆盖 conf 中某处的默认值:spring.devtools.restart.exclude
  • 您使用的是 Maven 还是 Gradle?如果是这样,你能提供这些的 pom.xml 或 build.gradle 文件吗?
  • @karen 当然可以。添加了 pom.xml 和 application.properties。

标签: java spring spring-boot spring-boot-devtools


【解决方案1】:

终于找到了解决办法。这比我想象的要简单得多。

由于我使用的是战争包装,我可以使用 webapp 的默认行为。 source 文件夹下的所有东西都会触发重启,但是 webapp 下的资源是在运行时获取的,所以不需要重新加载任何东西。

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

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

只是不要将默认的 Spring Boot 放置用于静态(基本上是资源下的任何东西),你会没事的。

【讨论】:

  • 我有一个类似的problem。感谢您对源文件夹的评论,它帮助我找到了解决问题的方法。
【解决方案2】:

您是否也阅读了“LiveReload”一章? 您需要确保您的 LiveReload Server 是从 SpringBoot-Devtools 启动的。您也可以使用来自LiveReload.com 的浏览器插件。 或者您可以使用来自Chrome Web Store 的插件。 详情请参阅章节LiveReload。

【讨论】:

  • 我无法使用 livereload 服务器,因为我正在使用 jsp 作为视图。因此我不能使用 jar 包装,我认为这是 livereload 所必需的。但是,据我所知,livereload 只需要在重新加载应用程序时实时更新浏览器。我需要的是避免重启,而不是触发刷新。
  • 嗯。不确定我是否理解正确,但您可以尝试将 livereload 标志设置为 false 并查看您的服务器本身是否支持热代码替换。你的application.properties 中的spring.devtools.livereload.enabled=false 应该这样做。
猜你喜欢
  • 2018-07-07
  • 1970-01-01
  • 2013-10-31
  • 2019-07-23
  • 1970-01-01
  • 2013-12-03
  • 1970-01-01
  • 1970-01-01
  • 2021-05-05
相关资源
最近更新 更多