Skip to content

Toro Cloud Dev Center

Martini package Spring context

Martini works on top of Spring and exposes much of the neat features that come with it to application developers. In this section of the documentation, we will discuss one of these re-exposed features: Spring IoC containers.


According to the Spring documentation:

The interface org.springframework.context.ApplicationContext represents the Spring IoC container and is responsible for instantiating, configuring, and assembling . . . beans. The container gets its instructions on what objects to instantiate, configure, and assemble by reading configuration metadata. The configuration metadata is represented in XML, Java annotations, or Java code. It allows you to express the objects that compose your application and the rich interdependencies between such objects.

Martini allows developers to define multiple configuration metadata in Martini packages and have Spring read them (as long as they are properly referenced) in order to create Spring beans and make them available within the scope of the package.

Simply put, a Spring bean is a Java object whose lifecycle is managed by a Spring IoC container, the environment in which Spring beans live. Spring beans defined in Martini are initialized and attached to the Spring context during package start-up, and will be available until the package is stopped (unless an error during initialization occurs).

Why should I let Spring manage my objects?

Objects that would be consumed or reused for composition throughout your project should ideally be instantiated as Spring beans. By doing so, you won't have to re-instantiate these objects whenever you need to use any of their methods.

It is very important to decide which objects should be instantiated as Spring beans as not all objects should be part of the Spring context (e.g. exceptions, enums, static utility classes).

Cross-package bean availability

As of the current version, beans added to the Spring context of a package are accessible across other started packages as well. This behavior may be subject to change in future versions of Martini.

Since Spring beans are just Spring-managed Java objects, their methods are still considered services in Martini; much like Groovy methods are Groovy services. This also means it's possible to select Spring bean methods in the service picker.

Service picker

The service picker appears when selecting a service for use with a component; usually the service which is invoked when conditions are met, such as when an endpoint gets triggered.


There are multiple ways to define Spring beans. The instructions below will describe how to create XML-based configuration metadata:

  1. Under your target Martini package's /conf directory, create an XML file. This XML file will contain the Spring bean definitions. It is possible to create more than one configuration metadata in order to organize your beans better. This file has no naming restrictions; although ideally, it should be named so that it is easy to identify.
  2. Edit the newly created XML file and define your Spring beans there. To help you get started fast and easy, here's a boilerplate from the Spring documentation which you can copy and paste unto the file; simply plug your <bean /> definitions as you like afterwards.

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns=""
        <!-- Define your beans here -->

    XML schema-based configuration

    See Spring's page on XML schema-based configuration for more examples and information about configuring Spring via XML.

  3. Update the package's package.xml file and add a reference to the XML file you created earlier by adding a new <context-file>${value}</context-file> element under the <spring-contexts> tag; ${value} being the base name of the XML file created earlier. For example:

    <package context-path="/examples"

    As an alternative, you can also use the package configuration editor to add your XML schema-based configuration.

    Adding Spring context files using the editor

    Add XML schema-based configuration files by dragging and dropping

    You can also drag and drop your XML schema-based configuration files to the Spring Context Files table in the package configuration editor.

    Adding Spring context files via dragging and dropping

    Adding Spring context files via dragging and dropping

  4. Restart the package for the context to be re-initialized.

Invoking Spring bean methods in Gloop

XML files added as package Spring context files are expandable in the Navigator. Under the XML file node will be the defined Spring beans and their methods. These methods can be invoked in a service through drag and drop, or content-assist.

Invoking a Spring bean's method in a service

Invoking a Spring bean's method in a service