{"id":15563942,"url":"https://github.com/mrsarm/spring-ctx","last_synced_at":"2025-12-26T22:59:58.576Z","repository":{"id":146923757,"uuid":"250954309","full_name":"mrsarm/spring-ctx","owner":"mrsarm","description":"contains the Java class `ctx.App`, that exposes the Spring context statically","archived":false,"fork":false,"pushed_at":"2020-06-09T15:35:42.000Z","size":97,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-19T05:48:37.299Z","etag":null,"topics":["java","jshell","spring","spring-boot"],"latest_commit_sha":null,"homepage":"https://mrsarm.github.io/spring-ctx/1.0/javadoc/index.html","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mrsarm.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-03-29T04:37:00.000Z","updated_at":"2024-04-19T13:55:51.000Z","dependencies_parsed_at":null,"dependency_job_id":"a3163eba-968d-4cf6-98a5-d94147d35142","html_url":"https://github.com/mrsarm/spring-ctx","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrsarm%2Fspring-ctx","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrsarm%2Fspring-ctx/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrsarm%2Fspring-ctx/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrsarm%2Fspring-ctx/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mrsarm","download_url":"https://codeload.github.com/mrsarm/spring-ctx/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243213985,"owners_count":20254902,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["java","jshell","spring","spring-boot"],"created_at":"2024-10-02T16:31:45.232Z","updated_at":"2025-12-26T22:59:58.529Z","avatar_url":"https://github.com/mrsarm.png","language":"Java","readme":"Spring Context\n==============\n\n`spring-ctx` contains the Java class `ctx.App`, that\nexposes the Spring context statically.\n\nYou can get a bean object from the context like the following in **Java**,\nwithout the need to inject it into your class:\n\n```java\nMyUserService myUserService = ctx.App.getBean(MyUserService.class);\n```\n\nBut the most important feature is to use it with the\n[jshell](https://docs.oracle.com/javase/9/jshell/introduction-jshell.htm) tool\nincluded in Java 9+ distributions, to access within the console\nto the Spring context, and therefore all the business objects created with it,\nlike many other frameworks allow to do, eg. the *Grails Console* in Groovy + Grails,\nthe *Django Admin Shell* in Python + Django, and the *Rails Console* in Ruby + RoR.\n\nTo do so, you need to start first a `jshell` console, start running\nyour application, and then use the `ctx.App` class to access your\nbean objects.\n\nDo you need to start the Jshell with all your deps in the classpath?\ncheckout the [jshell-plugin](https://github.com/mrsarm/jshell-plugin) project, it\nhas a special section that explains how to set up the plugin and\nthis library to play with Spring:\n[jshell-plugin - Spring Boot applications](https://github.com/mrsarm/jshell-plugin#spring-boot-applications)\n\n\u003e :information_source: Take a look to [spring-ctx-groovy](https://github.com/grayshirts/spring-ctx-groovy)\n\u003e for the same class but implemented in Groovy (not exactly the same class though).\n\nThe `ctx.App` class also exposes the properties of the project with the `prop` static method:\n\n```bash\njshell\u003e ctx.App.getProp(\"server.context-path\")\n$10 ==\u003e \"/api\"\n```\n\nWhen an object is returned, the jshell prints a representation of the\nobject (it calls the `toString()` method), but sometimes it's not the\nbest way to read the result, or you just need a JSON representation,\nin that case you can ask to the `ctx.App` class to get the\n[ObjectMapper](https://docs.spring.io/spring-boot/docs/current/reference/html/howto-spring-mvc.html#howto-customize-the-jackson-objectmapper)\nused by your application to print out results in JSON format:\n\n```bash\njshell\u003e var objMapper = ctx.App.getObjectMapper()\nobjMapper ==\u003e ObjectMapper\n\njshell\u003e System.out.println(objMapper.writeValueAsString(person))\n{\"name\":\"John\",\"lastName\":\"Doe\",\"age\":null}\n```\n\nOr you can call the convenient methods `pjson(Object)` and `ppson(Object)`\n(pretty print version) that allow to print the object using\nthe same object mapper mentioned above and then terminate the line:\n\n```bash\njshell\u003e ctx.App.ppjson(person)\n{\n  \"name\" : \"Jhon\",\n  \"lastName\" : \"Due\",\n  \"age\" : null\n}\n```\n\nYou can also access to the Spring context with `ctx.App.getContext()`, it\nreturns a [ApplicationContext](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/ApplicationContext.html)\ninstance, but the `App` class provides enough static methods to get\nthe bean objects, configuration properties, access the object mapper\nand the current environment (active profiles).\n\n:blue_book: Check the **javadoc** for more usage details:\nhttps://mrsarm.github.io/spring-ctx/1.0/javadoc/index.html\n\n\n:hammer: Configuration\n----------------------\n\nTo add the library to your project, depending on your building\ntool, these are the settings needed:\n\n### Gradle\n\nAdd the following configuration to the `build.gradle` file\nof your project:\n\n1. `dependencies` section:\n\n   ```groovy\n   implementation 'com.github.mrsarm:spring-ctx:1.0.0'\n   ```\n\n2. At the end of the `repositories` section:\n\n   ```groovy\n   maven { url 'https://jitpack.io' }\n   ```\n\n### Maven\n\nAdd the following configuration to the `pom.xml` file\nof your project:\n\n1. `dependencies` section:\n\n   ```xml\n   \u003cdependency\u003e\n       \u003cgroupId\u003ecom.github.mrsarm\u003c/groupId\u003e\n       \u003cartifactId\u003espring-ctx\u003c/artifactId\u003e\n       \u003cversion\u003e1.0.0\u003c/version\u003e\n   \u003c/dependency\u003e\n   ```\n\n2. At the end of the `repositories` section:\n\n   ```xml\n   \u003crepository\u003e\n       \u003cid\u003ejitpack.io\u003c/id\u003e\n       \u003curl\u003ehttps://jitpack.io\u003c/url\u003e\n   \u003c/repository\u003e\n   ```\n\n\nSystem Requirements\n-------------------\n\n * JDK 7+\n\n\nBuild \u0026 Publish\n---------------\n\nCompile and build the .jar locally with:\n\n```bash\n$ ./gradlew build\n```\n\nPublish to your local Maven repo:\n\n```bash\n$ ./gradlew publishToMavenLocal\n```\n\nPublish to the [JitPack](https://jitpack.io/) public repository:\njust release a new tag in the repository, and _JitPack_ will do\nthe magic !!\n\n\nAbout\n-----\n\n**Project**: https://github.com/mrsarm/spring-ctx\n\n:blue_book: **Javadoc**: https://mrsarm.github.io/spring-ctx/1.0/javadoc/index.html\n\n**Author**: Mariano Ruiz \u003cmrsarm@gmail.com\u003e\n\n**License**: [Apache Software License 2.0](https://www.apache.org/licenses/LICENSE-2.0).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmrsarm%2Fspring-ctx","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmrsarm%2Fspring-ctx","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmrsarm%2Fspring-ctx/lists"}