Is there a nice way to dependency inject using a jsp taglib?
either using ejb 3.0, spring, or guice...
I have a lot of services/pojos that I would like to use in my taglibs
Is there a nice way to dependency inject using a jsp taglib?
either using ejb 3.0, spring, or guice...
I have a lot of services/pojos that I would like to use in my taglibs
I think you want Seam, it enables you to refer to a component by name. However, the released version is JSF based, but that's changing.
在 servletContext 上保留对注入器的引用,然后根据需要在每个标记中使用。看
在您的 Guice 设置中:
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);
}
}
然后在你的标签库中:
@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.
只是偶然发现了你的问题,因为我也打算这样做。您实际上可以使用 Spring 及其 @Configurable 注释(使用 AspectJ 加载时或编译时编织)将服务注入到您的标签实现中。有关所有选项的详细说明,请在此处查看 Ramnivas 的博客文章。
希望在您仍然需要解决方案的情况下提供帮助...