Skip to content

contains the Java class `ctx.App`, that exposes the Spring context statically

License

Notifications You must be signed in to change notification settings

mrsarm/spring-ctx

Repository files navigation

Spring Context

spring-ctx contains the Java class ctx.App, that exposes the Spring context statically.

You can get a bean object from the context like the following in Java, without the need to inject it into your class:

MyUserService myUserService = ctx.App.getBean(MyUserService.class);

But the most important feature is to use it with the jshell tool included in Java 9+ distributions, to access within the console to the Spring context, and therefore all the business objects created with it, like many other frameworks allow to do, eg. the Grails Console in Groovy + Grails, the Django Admin Shell in Python + Django, and the Rails Console in Ruby + RoR.

To do so, you need to start first a jshell console, start running your application, and then use the ctx.App class to access your bean objects.

Do you need to start the Jshell with all your deps in the classpath? checkout the jshell-plugin project, it has a special section that explains how to set up the plugin and this library to play with Spring: jshell-plugin - Spring Boot applications

ℹ️ Take a look to spring-ctx-groovy for the same class but implemented in Groovy (not exactly the same class though).

The ctx.App class also exposes the properties of the project with the prop static method:

jshell> ctx.App.getProp("server.context-path")
$10 ==> "/api"

When an object is returned, the jshell prints a representation of the object (it calls the toString() method), but sometimes it's not the best way to read the result, or you just need a JSON representation, in that case you can ask to the ctx.App class to get the ObjectMapper used by your application to print out results in JSON format:

jshell> var objMapper = ctx.App.getObjectMapper()
objMapper ==> ObjectMapper

jshell> System.out.println(objMapper.writeValueAsString(person))
{"name":"John","lastName":"Doe","age":null}

Or you can call the convenient methods pjson(Object) and ppson(Object) (pretty print version) that allow to print the object using the same object mapper mentioned above and then terminate the line:

jshell> ctx.App.ppjson(person)
{
  "name" : "Jhon",
  "lastName" : "Due",
  "age" : null
}

You can also access to the Spring context with ctx.App.getContext(), it returns a ApplicationContext instance, but the App class provides enough static methods to get the bean objects, configuration properties, access the object mapper and the current environment (active profiles).

📘 Check the javadoc for more usage details: https://mrsarm.github.io/spring-ctx/1.0/javadoc/index.html

🔨 Configuration

To add the library to your project, depending on your building tool, these are the settings needed:

Gradle

Add the following configuration to the build.gradle file of your project:

  1. dependencies section:

    implementation 'com.github.mrsarm:spring-ctx:1.0.0'
  2. At the end of the repositories section:

    maven { url 'https://jitpack.io' }

Maven

Add the following configuration to the pom.xml file of your project:

  1. dependencies section:

    <dependency>
        <groupId>com.github.mrsarm</groupId>
        <artifactId>spring-ctx</artifactId>
        <version>1.0.0</version>
    </dependency>
  2. At the end of the repositories section:

    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>

System Requirements

  • JDK 7+

Build & Publish

Compile and build the .jar locally with:

$ ./gradlew build

Publish to your local Maven repo:

$ ./gradlew publishToMavenLocal

Publish to the JitPack public repository: just release a new tag in the repository, and JitPack will do the magic !!

About

Project: https://github.com/mrsarm/spring-ctx

📘 Javadoc: https://mrsarm.github.io/spring-ctx/1.0/javadoc/index.html

Author: Mariano Ruiz mrsarm@gmail.com

License: Apache Software License 2.0.