gpt4 book ai didi

java - CDI 注入(inject)抽象类

转载 作者:行者123 更新时间:2023-11-30 07:16:19 26 4
gpt4 key购买 nike

我有一组代码可以在 Glassfish 3.1.2 上的 JDK 1.6 中部署和执行得很好。但是,如果我们更新到 Glassfish 4 和 JDK 1.7,我们在尝试部署应用程序时会遇到以下错误:

org.jboss.weld.exceptions.IllegalArgumentException: WELD-001407 Cannot declare an injection point with a type variable: [BackedAnnotatedField] @Inject protected com.co.ejb.web.AjaxGridServlet.sourceInstance 
at org.jboss.weld.manager.InjectionTargetFactoryImpl.createInjectionTarget(InjectionTargetFactoryImpl.java:82)
at org.jboss.weld.manager.InjectionTargetFactoryImpl.createInjectionTarget(InjectionTargetFactoryImpl.java:68)
at org.jboss.weld.manager.BeanManagerImpl.createInjectionTarget(BeanManagerImpl.java:1039)
at org.jboss.weld.manager.BeanManagerImpl.fireProcessInjectionTarget(BeanManagerImpl.java:1197)
at org.glassfish.weld.WeldDeployer.firePITEvent(WeldDeployer.java:390)
at org.glassfish.weld.WeldDeployer.fireProcessInjectionTargetEvents(WeldDeployer.java:360)
at org.glassfish.weld.WeldDeployer.event(WeldDeployer.java:212)
at org.glassfish.kernel.event.EventsImpl.send(EventsImpl.java:131)
at org.glassfish.internal.data.ApplicationInfo.load(ApplicationInfo.java:328)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:493)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:219)
at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:491)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$2.execute(CommandRunnerImpl.java:537)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:546)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1423)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$1500(CommandRunnerImpl.java:108)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1762)
at org.glassfish.deployment.autodeploy.AutoOperation.run(AutoOperation.java:164)
at org.glassfish.deployment.autodeploy.AutoDeployer.deploy(AutoDeployer.java:595)
at org.glassfish.deployment.autodeploy.AutoDeployer.deployAll(AutoDeployer.java:482)
at org.glassfish.deployment.autodeploy.AutoDeployer.run(AutoDeployer.java:410)
at org.glassfish.deployment.autodeploy.AutoDeployer.run(AutoDeployer.java:401)
at org.glassfish.deployment.autodeploy.AutoDeployService$1.run(AutoDeployService.java:233)
at java.util.TimerThread.mainLoop(Timer.java:555)
at java.util.TimerThread.run(Timer.java:505)
Caused by: org.jboss.weld.exceptions.DefinitionException: WELD-001407 Cannot declare an injection point with a type variable: [BackedAnnotatedField] @Inject protected com.co.ejb.web.AjaxGridServlet.sourceInstance
at org.jboss.weld.bootstrap.Validator.checkFacadeInjectionPoint(Validator.java:765)
at org.jboss.weld.bootstrap.Validator.validateInjectionPointForDefinitionErrors(Validator.java:352)
at org.jboss.weld.injection.producer.InjectionTargetService.validateProducer(InjectionTargetService.java:39)
at org.jboss.weld.manager.InjectionTargetFactoryImpl.createInjectionTarget(InjectionTargetFactoryImpl.java:79)
... 24 more

有问题的文件是:

BaseJasonServlet

public abstract class BaseJsonServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doProcess(request, response);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doProcess(request, response);
}

protected void doProcess(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
this.process(request, response);
}

protected abstract void process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException;
}

AjaxGridServlet 扩展了 BaseJsonServlet。问题出在我们注入(inject) TSource 实例的文件中。

public abstract class AjaxGridServlet<TSource extends AjaxGridSource<?,?>> extends BaseJsonServlet {
private static final long serialVersionUID = 1L;

@Inject
protected Instance<TSource> sourceInstance;

@Override
protected void process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

TSource source= sourceInstance.get();
source.process(request, response);
response.getWriter().write(source.toJsonString());
}
}

AjaxGridServlet 然后由 webservlet 扩展

@WebServlet("/api/admin-dashboard")
public class AdminDashboard extends AjaxGridServlet<AdminDashboardSource> {
private static final long serialVersionUID = 1L;
}

如有任何帮助,我们将不胜感激。谢谢。

最佳答案

消息告诉您的是,您不能拥有带有类型变量的实例。也就是说,您不能使用 TSource那里。

你应该使用 Instance<AjaxGridSource>相反。

关于java - CDI 注入(inject)抽象类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17094880/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com