Table of Contents

Spring
Dependencies
Application context
Load-time weaving with AspectJ
Java annotation driven injection

Spring

OpenXava doesn't include internal mechanism for supporting Spring beans injection into IAction, ICalculator and other OX front-end implementors, but still by using Spring AOP in combination with AspectJ, it is possible to inject beans (e.g. beans from service layer - services) into OX dependent classes.

Dependencies

The next several jar dependencies must be included within OpenXava/web/WEB-INF/lib directory in order to use Spring features:
For load-time weaving purposes, these AspectJ libraries are required:
They should be also located within OpenXava/web/WEB-INF/lib directory.
Additional dependency is spring-agent.jar which will be passed externally to the JVM.
Note: In the time when this tutorial has been written, the actual version of Spring jars was 2.5.6 and for AspectJ jars it was 1.6.1.

Application context

In order to initialize Spring application context, few lines must be added into the OpenXava/web/WEB-INF/web.xml file, like in the example bellow:
<context-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Note: For more information on this, please check Spring Reference.

Load-time weaving with AspectJ

Load-time weaving (LTW) is simply binary weaving defered until the point that a class loader loads a class file and defines the class to the JVM. To support this, one or more "weaving class loaders", either provided explicitly by the run-time environment or enabled through a "weaving agent" are required. For this purpose Spring agent must be used and specified on the JVM startup, like in the example bellow:
java -javaagent:path/to/spring-agent.jar
Note: If you're using Tomcat application server, javaagent option can be specified through JAVA_OPTS parameter and the logical place for spring-agent.jar could be ${catalina.home}/server/lib.

Java annotation driven injection

Spring context module provides additional configuration through Java annotations so an application context XML might look like the example bellow:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"
    default-autowire="byName">
 
    <context:annotation-config />
    <context:spring-configured />
    <context:load-time-weaver />
    <context:component-scan base-package="org.openxava.springapp.frontend" />
 
    <bean id="exampleService" class="org.openxava.springapp.service.ExampleServiceImpl"/>
</beans>
When application context is configured like that, an action implementation that uses exampleService bean, might look like in the following example:
@Configurable
public class ExampleAction extends ViewBaseAction {
 
    private ExampleService exampleService;
 
    public void execute() throws Exception {
        exampleService.doSomething();
    }
 
    @Autowired
    public void setExampleService(ExampleService exampleService) {
        this.exampleService = exampleService;
    }
}
Each time when OpenXava instances ExampleAction class, LTW mechanism will listen for such event and set additional bean dependencies from the application context.
Note: Please refer to Spring Reference and AspectJ Guide for more information about LTW and annotation based configuration.