Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

contains the Java class `ctx.App`, that exposes the Spring context statically
https://github.com/mrsarm/spring-ctx

java jshell spring spring-boot

Last synced: 9 days ago
JSON representation

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

Awesome Lists containing this project

README

        

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:

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

But the most important feature is to use it with the
[jshell](https://docs.oracle.com/javase/9/jshell/introduction-jshell.htm) 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](https://github.com/mrsarm/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](https://github.com/mrsarm/jshell-plugin#spring-boot-applications)

> :information_source: Take a look to [spring-ctx-groovy](https://github.com/grayshirts/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:

```bash
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](https://docs.spring.io/spring-boot/docs/current/reference/html/howto-spring-mvc.html#howto-customize-the-jackson-objectmapper)
used by your application to print out results in JSON format:

```bash
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:

```bash
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](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/ApplicationContext.html)
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).

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

:hammer: 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:

```groovy
implementation 'com.github.mrsarm:spring-ctx:1.0.0'
```

2. At the end of the `repositories` section:

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

### Maven

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

1. `dependencies` section:

```xml

com.github.mrsarm
spring-ctx
1.0.0

```

2. At the end of the `repositories` section:

```xml

jitpack.io
https://jitpack.io

```

System Requirements
-------------------

* JDK 7+

Build & Publish
---------------

Compile and build the .jar locally with:

```bash
$ ./gradlew build
```

Publish to your local Maven repo:

```bash
$ ./gradlew publishToMavenLocal
```

Publish to the [JitPack](https://jitpack.io/) public repository:
just release a new tag in the repository, and _JitPack_ will do
the magic !!

About
-----

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

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

**Author**: Mariano Ruiz

**License**: [Apache Software License 2.0](https://www.apache.org/licenses/LICENSE-2.0).