|
From: Rod J. <rod...@in...> - 2003-12-24 17:30:48
|
All,
I've added a feature that should be useful in simple web applications.
Instead of defining all controller beans in the servlet XML file, I've added
a HandlerMapping implementation that automatically adds to the servlet's
bean factory all controllers having a PathMap attribute (see below for an
example of the source syntax). The PathMapHandlerMapping automatically
instantiates a singleton instance of the class, autowiring it via bean
properties (if there's a no-arg constructor) or constructor args. The
example shows dependency on a business object, defined in the servlet XML
file.
All you need to do is add the new PathMapHandlerMapping to your servlet XML
file. There's no need to define a bean or mapping for each controller.
Note that multiple attributes per controller class are supported, as shown
below. It's possible to combine this with the explicit mapping approach, so
it scales nicely to more complex requirements.
The attributes implementation is Commons Attributes, as presently used for
attribute-driven transaction management, another new feature. (See the
org.springframework.aop.framework.autoproxy.metadata tests, and the
/attributes directory of the PetStore sample app, for examples of this.)
See the comments in the
org.springframework.web.servlet.handler.commonsattributes package for
further details.
If there's enough interest, I'll add an example, including a build script.
Using Commons Attributes really doesn't add much complexity to the build
script (look at the PetStore /attributes/build.xml file), and the
precompilation step is very fast. I think source-level attributes are
potentially very useful in a number of areas.
To support this, I added a registerBeanOfClass() method to
AutowireCapableBeanFactory, which may be useful to those of you wanted to
plug in an instance of a new class, benefiting from autowiring.
Regards,
Rod
/**
*
* Normal comments here. The attribute below will automatically create
* a mapping from this path to an instance of this controller (shared for
all paths).
* Must use Commons Attributes compiler.
*
*
@@org.springframework.web.servlet.handler.commonsattributes.PathMap("/foo.cg
i")
*
@@org.springframework.web.servlet.handler.commonsattributes.PathMap("/baz.cg
i")
*/
public class FooController extends AbstractController {
private Cruncher cruncher;
public FooController(Cruncher cruncher) {
this.cruncher = cruncher;
}
protected ModelAndView handleRequestInternal(HttpServletRequest arg0,
HttpServletResponse arg1) throws Exception {
// Use cruncher
return new ModelAndView("test");
}
}
|