Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Dependency injection taglib->;依赖注入pojo/服务如何?_Dependency Injection_Taglib - Fatal编程技术网

Dependency injection taglib->;依赖注入pojo/服务如何?

Dependency injection taglib->;依赖注入pojo/服务如何?,dependency-injection,taglib,Dependency Injection,Taglib,有没有一种使用jsp标记库进行依赖项注入的好方法 使用EJB3.0、spring或guice 我有很多服务/POJO,我想在我的标记库中使用这些服务/POJO我想你需要,它允许你按名称引用组件。然而,发布的版本是基于JSF的,但这正在改变 我认为您需要,它允许您按名称引用组件。然而,发布的版本是基于JSF的,但这正在改变 刚刚偶然发现了你的问题,因为我也打算这么做。实际上,您可以使用Spring及其@Configurable注释(使用AspectJ加载时或编译时编织)将服务注入到标记实现中。有关

有没有一种使用jsp标记库进行依赖项注入的好方法

使用EJB3.0、spring或guice


我有很多服务/POJO,我想在我的标记库中使用这些服务/POJO

我想你需要,它允许你按名称引用组件。然而,发布的版本是基于JSF的,但这正在改变

我认为您需要,它允许您按名称引用组件。然而,发布的版本是基于JSF的,但这正在改变

刚刚偶然发现了你的问题,因为我也打算这么做。实际上,您可以使用Spring及其@Configurable注释(使用AspectJ加载时或编译时编织)将服务注入到标记实现中。有关所有选项的详细说明,请参阅Ramnivas的博客文章


希望在您仍然需要解决方案的情况下提供帮助…

刚刚偶然发现了您的问题,因为我也打算这样做。实际上,您可以使用Spring及其@Configurable注释(使用AspectJ加载时或编译时编织)将服务注入到标记实现中。有关所有选项的详细说明,请参阅Ramnivas的博客文章


希望在您仍然需要解决方案的情况下提供帮助…

在servletContext上保留对注入器的引用,然后根据需要在每个标记中使用。看

在您的GUI设置中:

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.

在servletContext上保留对注入器的引用,然后根据需要在每个标记中使用它。看

在您的GUI设置中:

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.