【问题标题】:What is the Grails Console of Spring Boot?Spring Boot 的 Grails 控制台是什么?
【发布时间】:2017-01-12 06:31:29
【问题描述】:

我最近开始在 Spring Boot 上花费更多时间,并且开始感觉到没有 shell(类似于 Grails 控制台)之类的应用程序,我可以使用存储库、服务等...弹簧靴?

【问题讨论】:

  • @DegenSharew 刚刚做到了。我认为这是在谈论没有 sb 容器的情况下访问 repos。我想要的是像 Grails 控制台这样的 GUI...我错过了 SO 线程中的某些内容吗?

标签: spring grails spring-boot


【解决方案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)

【讨论】:

  • 谢谢!我相信很多人都会依赖你的项目。
  • 不幸的是,因为这个项目依赖于在 Spring Boot 2.0+ 中停止的 Remote Shell,所以我的项目只适用于 Spring Boot 1.x 项目
【解决方案2】:

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

快速设置按照herehere的步骤:

  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 应用程序,从 Spring 上下文访问 bean 对象,如下所示:

$ 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 Wrapper (./gradlew),而不是已安装的 Gradle 版本 (gradle)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-18
    • 2016-07-02
    • 2011-06-12
    • 1970-01-01
    • 2017-03-03
    • 2019-06-13
    相关资源
    最近更新 更多