Adds a Clojure REPL to Spring applications to support interactive bug hunting.
To use, add the following snippet to your POM and build and run your Spring Boot application with profile
test-repl
.
<profiles>
<profile>
<id>test-repl</id>
<dependencies>
<dependency>
<groupId>de.sebhans.test</groupId>
<artifactId>spring-clojure-test-repl</artifactId>
<version>1.2.0</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</profile>
</profiles>
Putting the dependency in a Maven profile ensures that the dependency on Clojure does not pollute any builds (test, production, dev) unless explicitly enabled during the Maven build.
When running a Spring Boot application, activate the Spring profile test-repl
to automatically start a REPL listening on localhost:7888
.
The port can be changed by setting the property test.repl.port
to a different port.
The bind address can be changed by setting the property test.repl.bind-address
to a different value.
For a Spring-only application (without Spring Boot), you need to create a Bean of type de.sebhans.test.repl.REPLServer
yourself, passing the bind address and port as parameters to the constructor.
Since JDK 16, the following JDK options are required to make the reflection in Clojure work: --add-exports=java.base/sun.nio.ch=ALL-UNNAMED --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.lang.reflect=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-exports=jdk.unsupported/sun.misc=ALL-UNNAMED
Use your favorite nREPL client to connect to localhost:7888
.
The only clients which have been tested (because I use them) are
Monroe and CIDER
with GNU Emacs.
You might also try the Clojure-Kit IntelliJ plugin
or any client listed on the nREPL Clients web page.
Type Clojure code into the REPL and have fun.
This project includes the following Clojure libraries.
- nrepl 1.0.0 for provisioning of the nREPL
- cider-nrepl 0.30.0 for better integration with CIDER
- org.clojure/data.json 2.4.0 for JSON parsing and building
- http-kit 2.6.0 for HTTP requests
- enlive 1.1.6 for HTML parsing
Access to the Spring application context is provided via the static method REPLServer.getContext
,
which means you can access Spring beans from the REPL like this:
; Import REPLServer class so its methods are available in the REPL:
(import '(de.sebhans.test.repl REPLServer))
; Define helper function to access beans:
(defn get-bean
"Look up bean by name in the Spring application context"
[name]
(.getBean (REPLServer/getContext) name))
; Fetch a bean from the application context:
(def my-bean (get-bean "myBeanName"))
; Call a method on the bean
(.someMethod my-bean "these" "are" "the" "arguments")
See CHANGELOG.md.