Spring Boot的Grails控制台是什么?

5

最近我开始花更多的时间在Spring Boot上,并开始感受到缺少一个类似于Grails控制台的外壳(shell)应用程序,让我能够玩弄存储库、服务等等... 在Spring Boot中是否存在这样的东西?


你看过这个吗?https://dev59.com/gGox5IYBdhLWcg3wklCE? - dsharew
@DegenSharew刚刚发表了评论,我认为他是在谈论如何在不使用sb容器的情况下访问repos。但我想要的是像Grails控制台那样的GUI... 我在SO帖子里漏掉了什么吗? - Alexander Suraphel
2个回答

1
我最近推出了Spring Context from Groovy项目,它可以让你在repl groovy交互式控制台中使用Remote Shell插件来访问所有bean对象。
看起来像这样:
$ ssh -p 2000 user@localhost
user@localhost's password:
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::  (v1.4.6.RELEASE) on myhost
> repl groovy
Using repl groovy
> ctx.App.myUserService.findUserByEmail("mrsarm@gmail.com")[0].id
100123
> ctx.App.myUserService.getById(100123).name
Mariano Ruiz
> ctx.App.contactDao.logger.effectiveLevel
INFO
> ctx.App.contactDao.logger.setLevel(ch.qos.logback.classic.Level.DEBUG)

谢谢!我相信很多人会依赖于你的项目。 - Alexander Suraphel
很遗憾,由于这个项目依赖的远程Shell在Spring Boot 2.0+中已经停用,所以我的项目只能在Spring Boot 1.x项目中使用。 - Mariano Ruiz

0

正如我在之前的帖子和评论中提到的,您可以使用Groovy中的Spring Context工具来处理Spring Boot 1.x项目,但由于Spring Boot框架的更改,它不适用于Spring Boot 2+项目,在这种情况下,您可以使用jshell-plugin以及spring-ctx(这两个项目也是我创建的)。

快速设置请按照此处此处的步骤进行:

  1. 将以下内容添加到您的build.gradle文件中:

    plugins {
      id "com.github.mrsarm.jshell.plugin" version "1.1.0"
    }
    
  2. dependencies部分中添加以下依赖项:

    implementation 'com.github.mrsarm:spring-ctx:1.0.0'
    
  3. repositories部分的末尾添加以下内容:

    maven { url 'https://jitpack.io' }
    
  4. 任何Spring Boot应用程序都有一个带有@SpringBootApplication注释的类,它是应用程序的起点,并具有public static void main(String[] args)方法,您需要在项目根目录下创建一个名为startup.jsh的文件来调用该方法,例如:

    com.my.package.MyApplication.main(new String[]{})
    

    您还可以添加您要使用的业务类的导入,数量不限,否则您可以在启动JShell后导入它们:

    import com.my.package.services.MyUserService
    
  5. 配置完成。您可以在控制台中执行以下命令开始jshell会话:

    $ gradle --console plain jshell
    

一旦会话开始,您可以通过以下方式从Spring上下文中访问bean对象,与您的Spring应用程序交互:

$ gradle --console plain jshell

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.4.RELEASE)

14:43:28.320  INFO com.my.package.MyApplication             : Starting Application on kubu1910 with PID 5225 (/projects/...
14:43:28.323 DEBUG com.my.package.MyApplication             : Running with Spring Boot v2.2.4.RELEASE, Spring v5.2.3.RELEASE
14:43:28.324  INFO com.my.package.MyApplication             : The following profiles are active: ...
14:43:30.229  INFO boot.web.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8010 (http)

...
...

14:43:33.729  INFO tuate.endpoint.web.EndpointLinksResolver : Exposing 3 endpoint(s) beneath base path ''
14:43:33.811  INFO boot.web.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8010 (http) with context path ''
14:43:33.816  INFO com.my.package.MyApplication             : Started Application in 6.995 seconds (JVM running for 10.524)

> Task :jshell
|  Welcome to JShell -- Version 11.0.6
|  For an introduction type: /help intro

jshell> var myUserService = ctx.App.getBean(MyUserService.class)

jshell> ctx.App.ppjson(myUserService.getByUsername("admin"))
{
  "name" : "Jhon",
  "lastName" : "Due",
  "username" : "admin",
  "age" : null
}

最后建议:在Windows / Mac上,您可以使用rlwrap命令来使自动完成功能正常工作。对于Gradle存在的已知问题导致制表符和箭头键无法正常工作,因此您可以按照以下步骤调用JShell:

$ rlwrap gradle --console plain jshell

值得注意的是,如果项目中有可用的Gradle包装器(./gradlew),则可以使用它而不是已安装的Gradle版本(gradle)。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接