有没有一种很好的方式使用jsp taglib进行依赖注入?taglib - >依赖注入pojo /服务如何?
或者使用EJB 3.0,弹簧或吉斯...
我有很多服务/ POJO,唯一的,我想在我的标签库使用
有没有一种很好的方式使用jsp taglib进行依赖注入?taglib - >依赖注入pojo /服务如何?
或者使用EJB 3.0,弹簧或吉斯...
我有很多服务/ POJO,唯一的,我想在我的标签库使用
我想你想Seam,它可以让你按名称引用组件。然而,发布的版本是基于JSF的,但这种情况正在发生变化。
只是偶然发现你的问题,因为我打算这样做。实际上,您可以使用Spring及其@Configurable注释(使用AspectJ加载时或编译时编织)将服务注入到标记实现中。有关所有选项的详细说明,请参阅Ramnivas的博客文章here。
希望能在你仍然需要一个解决方案的情况下帮助...
保持对ServletContext的注入器的引用,然后在每个标签使用,你需要它。见
在你的吉斯设置:
public class GuiceServletConfig extends GuiceServletContextListener {
@Override
protected Injector getInjector() {
return Guice.createInjector(blah, blah);
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
ServletContext servletContext = servletContextEvent.getServletContext();
servletContext.removeAttribute(Injector.class.getName());
super.contextDestroyed(servletContextEvent);
}
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
Injector injector = getInjector();
ServletContext servletContext = servletContextEvent.getServletContext();
servletContext.setAttribute(Injector.class.getName(), injector);
super.contextInitialized(servletContextEvent);
}
}
然后在您的taglib:
@Singleton
@SuppressWarnings("serial")
public class MySampleTag extends TagSupport {
@Inject private MyInjectedService myService;
@Override
public int doStartTag() throws JspException {
Injector injector = (Injector) pageContext.getServletContext().getAttribute(Injector.class.getName());
injector.injectMembers(this);
String value = myService.doSomething();
etc.
etc.