春季启动后端+前端

时间:2018-04-17 23:01:59

标签: java spring maven spring-boot intellij-idea

我骑了关于spring dev工具和热重载的文档

https://docs.spring.io/spring-boot/docs/current/reference/html/howto-hotswapping.html https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-devtools.html#using-boot-devtools-restart-exclude

根据那是看到热备份java和支持的React / Typescript / webpack应用程序有这样的可能性

这是我们的架构(快捷方式)

mainmodule
    backendModules <- those modules are just maven project have theirs poms and etc
          backendModule1
          backendModule2
fontendModule
       content <- React/Typescript/Webpack/Less etc

backendModule2 - 我们用来启动后端 backendModule1 - 只是一些额外的服务 fontendModule / content - 是我们的全部反应应用

如果我说要为我们的前端文件重新加载,我是否正确:

  1. 配置Intellij,因为我正常为后端&lt; - 这很容易

    1a上。更改注册表

    1b中。自动选择构建项目

  2. 我的问题是我必须做什么来强制重新加载前端文件 - 所以开发人员只需运行1个应用程序然后后端+前端将自动重新加载

    1. 向资源模式添加前端扩展(Intellij:构建,执行 - &gt;编译器)? jsx,json,js,less等等?

    2. 根据doc添加“spring.devtools.restart.additional-paths”

    3. 有没有人能够做到这一点?我们没有得到任何错误等...

      如果有些事情似乎不清楚,请告诉我,以便我们澄清一下

1 个答案:

答案 0 :(得分:2)

我知道有两种不同的方法可以做到这一点。

1)使用Intellij文件观察器插件

2)运行webpack dev服务器作为spring-boot应用程序的反向代理

1)

  • 安装插件&#34;文件监视器&#34;来自知识库
  • 转到设置 - 工具 - 文件监视器
  • 定义Webpack任务
    • 文件类型:任何
    • 定义新范围以仅查看您的javascript文件
    • 程序:从/node_modules/webpack/bin/webpack.js运行webpack 或创建一些其他可执行文件(如$ ProjectFileDir $ / webpack.sh)
    • 如果你选择从node_modules运行webpack(由于相对路径问题,btw对我没用)插入参数(如--config)
    • 设置刷新输出路径:$ ProjectFileDir $ / src / main / static / js / bundled
    • 将工作目录设置为:$ ProjectFileDir $

现在,如果您在定义的范围内保存文件,则会运行webpack任务。之后,您必须在浏览器中刷新页面。从https://intellij-support.jetbrains.com

得到了这个想法

2) 这种方式更先进,但更难配置正确。您将自动重新加载(页面刷新)和完全热重新加载(反应状态仍然存在)

基本思想是运行webpack-dev-server并将该服务器用作spring-boot后端的反向代理。开发服务器将自己处理热重新加载内容的请求,并将其他所有内容传递给您的后端。从https://www.codingbismuth.com/获得了这个想法。

作为示例配置

&#13;
&#13;
{
  "name": "",
  "version": "0.0.1",
  "description": "",
  "repository": {
    "type": "git",
    "url": ""
  },
  "keywords": [
    "xy"
  ],
  "author": "murphy",
  "license": "",
  "bugs": {
    "url": ""
  },
  "scripts": {
    "start:dev": "webpack-dev-server --config webpack.dev_server.js"
  },
  "homepage": "",
  "dependencies": {
    "file-saver": "^1.3.3",
    "prop-types": "^15.5.10",
    "react": "^16.2.0",
    "react-bootstrap-typeahead": "^2.3.0",
    "react-dom": "^16.2.0",
    "react-modal": "^3.1.8",
    "react-router": "^4.2.0",
    "react-router-dom": "^4.2.2",
    "react-datetime": "^2.11.1",
    "rest": "^1.3.1",
    "moment": "^2.20.1",
    "webpack": "^3.10.0",
    "swagger-ui": "^3.13.4",
    "webpack-dev-server": "^2.11.2"
  },
  "devDependencies": {
    "webpack-cli": "^2.0.15",
    "react-hot-loader": "^4.1.2",
    "babel-core": "^6.18.2",
    "babel-eslint": "^8.0.3",
    "babel-loader": "^7.1.2",
    "babel-polyfill": "^6.26.0",
    "babel-preset-es2015": "^6.18.0",
    "babel-preset-react": "^6.16.0",
    "eslint": "^4.13.1",
    "eslint-plugin-react": "^7.5.1",
    "eslint-loader": "^1.9.0",
    "eslint-watch": "^3.1.3",
    "eslint-config-airbnb": "^16.1.0",
    "eslint-plugin-jsx-a11y": "^6.0.2",
    "eslint-plugin-import": "^2.8.0",
    "eslint-plugin-flowtype": "^2.40.1",
    "uglifyjs-webpack-plugin2": "^1.0.3"
  }
}
&#13;
&#13;
&#13;

&#13;
&#13;
const { resolve } = require('path');
const path = require('path');
const webpack = require('webpack');

module.exports = {
    context: resolve(__dirname, '.'),

    entry: [
        'react-hot-loader/patch',
        // activate HMR for React

        'webpack-dev-server/client?http://localhost:8888',
        // bundle the client for webpack-dev-server
        // and connect to the provided endpoint

        'webpack/hot/only-dev-server',
        // bundle the client for hot reloading
        // only- means to only hot reload for successful updates

        // the entry point of our app
        './src/main/js/router/mainrouter.jsx',
    ],
    output: {
        filename: './mainbundle.js',
        // the output bundle

        path: resolve(__dirname, '/src/main/resources/static/js/bundled'),

        publicPath: '/js/bundled/',
        // necessary for HMR to know where to load the hot update chunks
    },

    devtool: 'sourcemaps',
    devServer: {
        hot: true,
        contentBase: [resolve(__dirname, "."), resolve(__dirname, "./src/main/resources/static/js/bundled")],
        proxy: {
            "/": {
                target: {
                    host: "localhost",
                    protocol: 'http:',
                    port: 8087,
                },
            },
            ignorePath: true,
            changeOrigin: true,
            secure: false,
        },
        publicPath: '/js/bundled/',
        port: 8888,
        host: "localhost",
    },
    module: {
        rules: [
            {
                enforce: "pre",
                test: /\.jsx$/,
                exclude: /node_modules/,
                loader: "eslint-loader",
                options: {
                    // fix: true, // autofix
                    cache: true,
                    failOnError: false,
                    emitWarning: true,
                    quiet: true,
                },
            },
            {
                test: path.join(__dirname, '.'),
                exclude: /node_modules/,
                loader: "babel-loader",
                query: {
                    // cacheDirectory: true,
                    presets: ['es2015', 'react'],
                },
            },
            {
                test: /\.css$/,
                use: ['style-loader', 'css-loader?modules'],
            },
        ],
    },

    plugins: [
        new webpack.HotModuleReplacementPlugin(),
        // enable HMR globally

        new webpack.NamedModulesPlugin(),
        // prints more readable module names in the browser console on HMR updates
    ],
};
&#13;
&#13;
&#13;

Spring boot应用程序运行于:8087,webpack dev服务器运行于:8888。现在在index.html中包含mainbundle.js。 运行spring-boot应用程序并在第二个终端运行中运行:

npm run start:dev

访问:8888上的网页,以便在文件更改时进行热重新加载。

相关问题