Spring usage in inspectIT components

The Spring Framework is used extensively in all inspectIT components. Currently we still have some differences between the components. This page tries to document the different ways we use Spring

Commons / CommonsCS

Both projects are Spring-enabled. Although they both do not start a Spring context, one can assume that all services are used from a spring container. Thus you can use Spring annotation like @Autowired to mark fields to be injected by Spring.

Agent

The agent is fully Spring annotation enabled. Spring beans do not have to be configured in any configuration file, but with common Spring annotations (like @Service)

CMR

The CMR is fully Spring annotation enabled as well, but also defines number of beans via the .xml files as well. The current files (stored in src/spring) are:

  • spring-context-global.xml - Global settings, enable the annotation based component scan, deals with properties holder, etc.
  • spring-context-beans.xml - Contains beans that are created via factories or define lookup methods. It is expected that in future this file is removed and defined beans configured via annotations.
  • spring-context-database.xml - Defines database related beans and settings. 
  • spring-context-jetty.xml - Defines the settings for the Jetty web-server that is started on the CMR.
  • spring-context-processors.xml - Contains definitions for all implementations of the AbstractCmrDataProcessor that will be used on the CMR. Since we have specific rules how we order and chain processors, we can not let Spring randomly create the processor collection via annotations. If you are writing a new data processor it's definitely a place where it needs to be defined.
  • spring-context-rest.xml - Defines settings for our Rest service

If you are developing a new bean for the CMR in most cases you should go for the annotation definition via @Component or similar.

User Interface

The user interface runs within an OSGI container. This makes the direct use of Spring a bit difficult, since all definition must be made explicitly via XML files. The files are located in the META-INF/spring folder:

  • spring-context-model-main.xml - Considered as the main definition that holds all necessary beans in order to correctly communicate with CMR. Also used in the inspectIT JMeter project, thus add a definition here if it's dependency for that project as well. Don't add any bean that depends on the RCP classes (even if it imports it) here, as this brings us risk of having problems in inspectIT JMeter project (use spring-context-model-other.xml).
  • spring-context-model-storage.xml - Contains storage related beans. 
  • spring-context-model-other.xml - Defines any other beans, especially those that have dependencies on the RCP framework in same case.
  • spring-context-model-osgi.xml - Defines OSGi exposed services.

Note that annotation scanning will not work on the User interface. Any new bean must be registered in the XML definition. Furthermore, if bean has other Spring related dependencies, then setters in the new bean are also required for dependency injection to work.

Note that any spring bean can be publish as OSGi service and can afterwards be used through the InspectIT class.

VersionService versionService = InspectIT.getService(VersionService.class)

Please note that in order for the injection to work you need to:

  1. Define the bean (in this example the version service) as a spring bean in one of the context files
  2. Publish the bean as osgi service in spring-context-model-osgi