Modular web application with Spring and Maven
Asked Answered
F

2

10

I'm trying to design the architecture of a medium-sized web application in Java and I would like to get some advice on how to do it.

The project consists on a base website plus a number of modules. For instance, one module would provide user registration, another module would offer a web service, and so on...

Whenever I need to deliver the application to a new customer, the ideal thing would be to pick up the modules he wants, do some theming (css, images, maybe jsp) and developing the custom modules he may need, if any.

I've taken a look to maven multi module projects, war overlays, but I find it difficult to partition the application, especially in regard to the modules' configuration (for example, merging a global spring configuration from the modules). Can somebody point me to an example of such a system? Thanks in advance!

Frambesia answered 18/2, 2013 at 22:24 Comment(0)
W
6

merging spring configuration is easy. In each module, package up a spring context file in it's /WEB-INF/classes directory. When you overlay, all classes and resources in WEB-INF classes in the dependency will be put into WEB-INF/classes in your app. (ps, this also works if you package as .jar instead, but you won't be able to overlay .jsp files if you do)

Then it's just a matter of importing them. This is best done by using a set pattern to find the files. Here's an example:

<import resource="classpath*:/module/*-context.xml" />

This will import all the classpath resources that match this pattern.

Annotation based example:

@Configuration
@ImportResource(value={"classpath*:/module/*-context.xml"})
public class MyConfiguration { ... }

It's the web.xml configuration that will cause you more trouble than anything if you have the need to do any web.xml customizations in modules. You could use servlet 3.0 for this of course, but it requires the right server to deploy on.

Watthour answered 19/2, 2013 at 1:55 Comment(2)
thanks for helping! I see the point here is to "fragment" configuration files across modules, when possible. I think I could live with a single web.xml. So I'm now going to give this a try and see what can be done with the other technologies configuration (hibernate, tiles and so on). I'll update this.Frambesia
hibernate can have their models fragmented too. the spring factory beans for hibernate session factories have classpath scanning as well (either .hbm.xml or package based scanning)Watthour
D
1

fwiw, after some experience with plain Spring import we developed tiny framework for osgi-less modularity for Spring. The first problem with import is the bean name clashes you can not have same-named singleton in two contexts, and many other hassles.. tbc https://github.com/griddynamics/banshun

-- Mike

Dewy answered 30/8, 2013 at 21:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.