It's very possible
.
1) Make sure your tool implements org.apache.velocity.tools.view.tools.ViewTool
2) Implement ViewTool's public void init(Object obj) method something like so:
Code:
public void init(Object obj) {
logger.debug("initializing " + this.getClass().getName());
ServletContext servletContext = (ServletContext) obj;
WebApplicationContext ctx = WebApplicationContextUtils
.getRequiredWebApplicationContext(servletContext);
Integer test = (Integer) ctx.getBean("test");
logger.warn("test from ctx (should be 1):" + test);
}
3) Define the tool in your Velocity toolbox.xml file - for the example above, define it like:
Code:
<tool>
<key>testtool<key>
<scope>session</scope>
<class>your.Tool</class>
</tool>
All of the above assumes your tool will have session (or application) scope (as defined in toolbox.xml). If you give it request scope, I believe the object that is passed into the init() method will be of type HttpServletRequest.
Good luck!
Jon