Using Spring Features in Z2

This section highlights the integration of Spring with z2 based on the integration features implemented in the project “com.zfabik.springframework” as well as by the module separation choice for the Spring framework.

A Simple Sample Application

We will refer to the simple example application consisting of the projects

samples.spring.simplemodules.frontend and samples.spring.simplemodules.services

that you can find in the z2@spring repository.

This example illustrates the use of integration features with the z2 environment such as:

  • The Component Factory Bean.
  • Exposing Spring beans and Spring application contexts as z2 components.
  • Use of the Spring AspectJ aspect with z2.

Spring Modules

The Spring framework has been split into different projects and components for use in z2 - all starting with org.springframework.

org.springframework.foundation

This project contains the most commonly used Spring libraries and exposes them by its Java component.

In addition it provides Java files projects for inclusion into the referencing component. Please review IJavaComponent for the details about referencing files components from a Java component. Also visit Advanced Notes for more details on why and how to avoid classloading issues when using Spring in Z2 (and any other non-trivial class loading setup).

In essence, some Spring libraries need to run within the class loading context of the using component and cannot be shared during runtime.

The component org.springframework.foundation/context.support provides a number of adapter classes that allow simple use of for example Velocity templates in Spring applications. As a consequence the adapter classes in Spring Context Support require to see the relevant third-pary libraries at runtime – depending on the use case. In short, the library must run in the using class loading context to resolve the type dependencies. Otherwise the Spring foundation project would need to have all the missing dependencies (and hence z2@Spring would have to hold ALL supported third-party libs.

Similarly the component org.springframework.foundation/aspects provides the Spring AspectJ aspect as a “files” component. In this case, it is the way the aspect refers to the Spring application context that mandates to use it as a “classpath singleton” from the using component. Or simply put: It cannot be shared on the classpath.

Note #1:

In order to use the foundation package, reference it as org.springframework.foundation or org.springframework.foundation/java.

Note #2:

In order to use the context support package, additionally reference org.springframework.foundation/context.support. If your project exposes an API, it is recommended to reference the context support component only as a private reference, i.e. from the Java component's implementation part.

Note #3:

Using the Spring AspectJ aspect and hence Spring automatic annotation-driven configuration for beans that are instantiated outside of the application context you need to

  1. Reference org.springframework.foundation/aspects - as above it is strongly recommended to do so only from the implementation part of a Java component, i.e. via a private reference (please see Advanced Notes).

  2. Additionally reference javax.persistence. The Spring aspect has a built-in dependency on JPA API classes.

  3. Make sure the AspectJ compiler is invoked by adding the line
    java.compile.order=java,spring_aspectj
    to the Java component properties (see also ICompiler).

 

Example:

This feature is used in the controller servlet implementation in samples.spring.simplemodules.frontend/web. While the servlet is instantiated by the web container, i.e. outside of Spring direct control, the Spring aspect still associates it with the application context of the web app as we the class is annotated correspondingly:

@Configurable
public class ControllerServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Autowired
    private IComputationService computations;


...

org.springframework.jdbc, org.springframework.orm, org.springframework.transaction, org.springframework.web

The projects simply expose the corresponding Spring module as shared Java components including required references.

org.springframework.security

The Spring Security project consists of some exposed Java libraries for shared use and a lot of “files” components. The files component serve similar integration or AOP purposes as described above for the Spring foundation project. In other words, if you want to use the Spring Security LDAP support, you should reference the corresponding files component from the Spring Security project.