– Java frontend with the Standard Web Toolkit (SWT), started via Java Web Start. – Spring Remoting as the interface to the backend. – Spring web application on a Tomcat as backend.

The backend is standard Spring, and who doesn’t know what Spring Remoting is can read it up here. Here I wanna talk about the combination of Spring and SWT in the frontend.

So, what are the goals? We want the UI components to be as dumb as possible, because they are a little harder to test. Everything that smells like business logic should be located in easy-to-test-POJOs. Components should be wired up via dependency injection, removing the need for service locators in the code. I had no experience with SWT, so my first naive thought was, good, SWT UI components are becoming Spring beans, services on the client side are Spring Remoting proxies, and between the services and the UI components we add a layer of controllers containing any business logic.

It wasn’t that easy.

SWT UI components have their own lifecycle, they may be created and disposed at any time, and recreated again. The scope Singleton would definitely have been the wrong one. Okay, I thought, then the UI components would be prototype beans, and whenever we need one, we create it.

Even that’s easier said than done. Let’s take a look at a typical UI Component.

This component is a view with a list that can be filtered via a filter component. The list is displayed in a tab. SomeFilterComponent and SomeSortableGrid are proprietary UI components as well. As you can see, the parent component is always a constructor argument to the child component. We have a bidirectional dependency here that makes dependency injection hard: if you want to inject SomeFilterComponent into SomeListView, you would have to create SomeListView before, so that it can be used in the constructor of SomeFilterComponent. That would still be possible with Singletons, but taking into account that both components need to be Prototypes it gets impossible.

Conclusion: SWT – UI components cannot become Spring beans.

What now? Good old service locators again?

No, there is an elegant solution with a little bit of AspectJ magic needing very little effort. Here the three steps involved:

1. Include the Maven-AspectJ-Plugin for compile-time-weaving in our pom 2. Use @Configurable and @Autowired in our UI component classes 3. Activate dependency injection in the application-context.xml

This way we can inject Spring beans in normal objects (our UI components) created via normal constructor invocation.

Let’s go into the details:

1. Include the Maven-AspectJ-Plugin for compile-time-weaving in our pom

Having this configuration included in the pom the aspects from spring-aspects will be woven into the classes. We are interested in the AnnotationBeanConfigurerAspect, that will be woven into each class annotated with @Configurable. Now we can use the @Autowired annotation to inject dependencies in those classes, which leads us directly to the next point.

@Configurable is telling AspectJ to weave the AnnotationBeanConfigurerAspect into those classes. When you set ‘preConstruction = true’ as an annotation parameter, dependencies will even be injected before the constructor of the class is called. That’s why we can use SomeController in the constructor here. Dependencies you want to inject have to be annotated with @Autowired, like it is done here with the SomeController.

3. Activate dependency injection in the application-context.xml

The AnnotationBeanConfigurerAspect being woven in doesn’t mean that it can get active right away. Aspects are static usually, so we have to expose our ApplicationContext in a static place the aspect knows. Spring is doing this transparently when we add the following to the ApplicationContext xml file:

<context:spring-configured/>

For activating the autowiring via annotations we need the following entry:

<context:annotation-config/>

That’s it. Now we can create SomeListView via constructor and the dependencies get injected automatically. No service locator, no glue code. Everything smelling like business logic can be sourced out into controllers (or however you wanna name them) being injected into the UI component. The UI component itself stays as simple as possible.

One additional word regarding testability

If you take a look again at the code of SomeListView, you’ll notice a Nullpointer being thrown whenever you instantiate the class without having an application context configured in the right way. We accepted that you cannot test SWT – UI components easily in a standard unit test (that’s nothing about Spring – since the UI components are coupled tightly through the constructor calls, unit testing is impossible (to be clear: I’m talking about unit tests, not JUnit tests)). For testing an UI component we use the following base class: