将依赖项注入taglib类?

时间:2010-12-08 16:26:55

标签: java spring taglib

我正在使用Spring 3,并希望将一些依赖项注入到作为taglib一部分的类中。我可以想象使用构造函数arg的一些kludge,但我希望其他人有更好的主意。

2 个答案:

答案 0 :(得分:6)

如果您决定从自定义标记访问服务或DAO,则需要从标记访问ApplicationContext,然后获取Bean。

ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(pageContext.getServletContext()); 
MyService myService = applicationContext.getBean(MyService.class);
myService.doSomething();

答案 1 :(得分:1)

还有http://www.shredzone.org/projects/jshred/wiki/Spring_supported_Tag_Libraries - 这可以通过为每个taglib类创建一个代理类来处理与Spring的交互,并在需要时使用Spring生成taglib的实例。