Chapter 8. Third-party Integration

8.1. Spring Application Framework

8.1.1. BeanFactory, ApplicationContext, WebApplicationContext

AraneaSpringDispatcherServlet will always add a BeanFactory to the environment. It can be retrieved as follows:

BeanFactory beanFactory = 
  (BeanFactory) getEnvironment().getEntry(BeanFactory.class)

Or using the method getBeanFactory() in BaseUIWidget. By default it will contain only beans configured by Aranea, however if one also uses usual Spring configuration:

...
<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value> 
    /WEB-INF/services.xml
  </param-value>
</context-param>
...
<listener>
  <listener-class>
    org.springframework.web.context.ContextLoaderListener
  </listener-class>
</listener>
...

Then the AraneaSpringDispatcherServlet will integrate with Spring and make BeanFactory provide all of the configure beans, as well as add ApplicationContext and WebApplicationContext to the environment.

Warning

AraneaSpringDispatcherServlet must be initialized after Spring context loader listener or servlet to integrate with Spring successfully.

8.1.2. Spring Localization Filter

Java class:SpringLocalizationFilterService
Default configuration name:-
Provides:LocalizationContext, SpringLocalizationContext
Depends on:WebApplicationContext

Provides localization services, see Section 2.8.2, “LocalizationContext”. The difference from the usual localization filter is that this one delegates the actual localization to a Spring MessageSource.

8.1.3. Widget Dependency Injection

Aranea does not by default support configuring widgets with Spring, as they are assumed to be created by the programmer and their life-cycle is managed by Aranea. The main problem however is that widgets are assumed to be serializable and Spring beans are often not (especially since they often are proxies with references to bean factory and so on). As a solution we provide a utility class SpringInjectionUtil that allows to inject Spring beans after a following convention:

...
injectSomeSpringBean(ISomeBean someBean) {
  this.someBean = someBean;
}
...

This method is similar to a setter method, but starts with "inject". The remainder of the method name is interpreted as the name of Spring bean to be injected, with the first letter lowercase (in the case of our example bean named "someSpringBean" would be injected). To actually inject the beans to all similarly called methods in the current widget call injectBeans() in widget init() method as follows:

...
protected init() {
  ...
  SpringInjectionUtil.injectBeans(getEnvironment(), this);
}
...

You may even put this call into the base widget of your application to ensure that all application widgets would get their dependencies injected.

Note

The injected bean must be an interface, as Aranea will construct an indirection proxy. This will ensure that the referenced object will be serializable (and small for that matter), but will also introduce a small performance penalty (we believe to be negligible next to the proxies of Spring itself).