Skip to content

Latest commit

 

History

History
73 lines (59 loc) · 3.42 KB

README.md

File metadata and controls

73 lines (59 loc) · 3.42 KB

spring-clojure-test-repl

Adds a Clojure REPL to Spring applications to support interactive bug hunting.

Integration

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

Usage

Connecting to the REPL

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.

Available Functions

This project includes the following Clojure libraries.

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")

Changelog

See CHANGELOG.md.