![](/img/trans.png)
[英]Use Spring Boot Spring Security configuration in Grails 3
[英]What is the Grails Console of Spring Boot?
我最近開始在 Spring Boot 上花費更多的時間,並且開始感覺沒有像應用程序這樣的 shell(類似於 Grails 控制台),我可以使用存儲庫、服務等......在 spring boot 中是否存在這樣的東西?
我最近從 Groovy項目啟動了Spring Context ,它使您可以使用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)
正如我在上一篇文章和后面的評論中提到的,您可以將Groovy工具中的Spring Context用於 Spring Boot 1.x 項目,但是由於 Spring Boot 框架的變化,它不適用於 Spring Boot 2+ 項目,在這種情況下,您可以將jshell-plugin與spring-ctx (我也創建的兩個項目)一起使用。
將以下內容添加到您的build.gradle
:
plugins { id "com.github.mrsarm.jshell.plugin" version "1.1.0" }
將以下依賴項添加到dependencies
部分:
implementation 'com.github.mrsarm:spring-ctx:1.0.0'
在repositories
部分的末尾添加:
maven { url 'https://jitpack.io' }
任何 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
配置完成。 您可以啟動在控制台中執行的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
) 版本。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.