英文:
spring boot dev tools
问题
我正在使用Spring Boot编写一个REST API,可以在运行时生成类、控制器和存储库。我使用开发工具在生成代码后重新编译该类。
我已经在我的pom.xml中添加了开发工具的依赖,在我的本地机器上一切正常运行。然而,一旦我部署到Ubuntu服务器并在运行时创建新的类,当我尝试访问端点时会生成错误。
{
"timestamp": "2020-04-05T03:26:53.733+0000",
"status": 404,
"error": "Not Found",
"message": "No message available",
"path": "/api/reg"
}
当我关闭Tomcat服务器并重新运行应用程序时,先前生成的类及其控制器变得可用,我可以访问端点。
因此,我的问题是:如何使Spring Boot在运行时生成类后能够重新编译和扫描所有生成的组件,而无需关闭服务器?
英文:
I'm writing a REST API in spring boot that generates a class, controller and repository at runtime. I'm using dev tools to recompile the class once the code is generated.
I have included the dev tools dependencies in my pom.xml and everything works fine on my local machine. However, once I have deployed to an Ubuntu server and I create a new class at runtime it generates an error when I try to hit the endpoint.
{
"timestamp": "2020-04-05T03:26:53.733+0000",
"status": 404,
"error": "Not Found",
"message": "No message available",
"path": "/api/reg"
}
When I shutdown the Tomcat server and re-run the application the previously generated class and its controller become available and am able to hit the endpoint.
So my question is; how can I make spring boot to re-compile and scan all the generated components once class is generated at runtime without having to shutdown the server?
答案1
得分: 0
问题可能在于您的本地环境中使用了某种嵌入式的Tomcat或Jetty。它可以在运行时检测到更改。
我假设您使用Tomcat作为Web服务器(而不是与jar包本身一起提供的嵌入式服务器),因为您提到您必须手动重新启动它。不确定Tomcat Web服务器是否能够在运行时检测到类的更改。
如果您希望使Tomcat能够在运行时重新加载类,请参考这个答案。
英文:
The problem might be that on your local you use some sort of embedded Tomcat or Jetty. It's able to pick up changes un runtime.
I assume that you use Tomcat as a web server (not embedded one that comes with jar itself) since you mentioned that you have to reboot it manually. Not sure that the Tomcat web server is able to pick up class changes in runtime.
Try to look at this answer if you want your Tomcat to reload classes in runtime.
答案2
得分: 0
spring-boot-devtools是一个有用的功能,当在哪个集成开发环境(IDE)中工作时,它为代码更改提供了非常快速的反馈循环。请注意,在运行完全打包的应用程序时,开发工具会自动禁用。
当使用java -jar部署/启动应用程序时,它会被禁用;或者如果使用特定的类加载器触发它,也会被禁用。然后它被视为生产应用程序。
您可以将该依赖标记为可选,这是一种最佳实践,可防止将devtools应用于其他模块。您可以在此Spring文档中查阅更多详细信息:
英文:
spring-boot-devtools is an useful feature when working which IDE as it gives a very fast feedback loop for code changes and please be noted that the dev tools are automatically disabled when running a fully packaged application.
It is disabled when it is deployed/launched using java -jar or if it is triggered using a specific class loader. Then it is considered to be a production application.
You can flag the dependency as optional which is the best practice that prevents devtools from being applied to other modules. You can refer further details at this spring documentation,
专注分享java语言的经验与见解,让所有开发者获益!
评论