Skip to content

Martini Package Spring Context

Introduction

Martini integrates the Spring Framework to enhance its application development capabilities. This section explores the Spring IoC containers and their utilization within Martini packages.

Advantages of Spring IoC Containers in Martini

Spring's ApplicationContext is instrumental in managing object instantiation, configuration, and assembly. Integration with Martini offers several benefits:

  • Efficient Object Management: Managing frequently used objects as Spring beans simplifies their instantiation and lifecycle management.
  • Lifecycle Oversight: Spring beans in Martini are initialized at package start-up and remain active until package termination, optimizing resource use.
  • Cross-Package Bean Access: Beans in one package can be accessed in other started packages, enhancing modularity and reuse. This behavior may evolve in future Martini versions.

Implementing Spring Beans in Martini

  1. Create an XML Configuration File: In the /conf directory of your Martini package, create an XML file for Spring bean definitions. Name it descriptively for easy identification.

  2. Define Your Beans: Edit the XML file to include your Spring bean definitions, following a basic structural template from the Spring documentation.

  3. Update Package Configuration: Reference the new XML file in your package's package.xml under the <spring-contexts> section. This can also be done using Martini's package configuration editor. Simply right click the package in the Navigator select Configure. Scroll down to the Spring Context Files section of the package configuration, click the + button and then add a reference to the XML file you created in Step 1.

  4. Restart Your Package: To apply the new configurations, restart your package.

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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
```xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- Define your beans here -->

</beans>
```

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

Utilizing Spring Bean Methods

Spring beans and their methods, once defined, are accessible under the respective XML file in the Navigator. Expand the XML file in the Navigator to show the defined Spring beans and their methods. These methods can be invoked in Martini services through drag and drop, or via content-assist.