在Java 8上,我无法继承和覆盖继承的注释

时间:2015-03-03 11:41:05

标签: java java-8 cdi

我有一个注释

@Qualifier
@Inherited
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface InheritedQualifier {
     public String value();
}

我有四个使用它的类。

  • 拥有@InheritedQualifier("original")
  • 的家长
  • 扩展Parent但不做修改的子女
  • GrandChild,它扩展了Parent并拥有@InheritedQualifier("override")
  • GreatGrandChild扩展GrandChild但不做任何修改

所有四个类都使用唯一的名称实现@Named。最后我有一个第五课,注射如下:

@Inject @InheritedQualifier("original") @Named("parent") Parent parent;
@Inject @InheritedQualifier("original") @Named("child") Child child;
@Inject @InheritedQualifier("override") @Named("grandChild") GrandChild grandChild;
@Inject @InheritedQualifier("override") @Named("greatGrandChild") GreatGrandChild greatGrandChild;

现在这里很奇怪。当我在Oracle的Java7上运行它时,它运行正常。当我在Java8上运行它时,它无法注入GreatGrandChild。这是错误消息:

javax.enterprise.inject.UnsatisfiedResolutionException: Api type [com.ibm.ws.fat.jcdi.inheritance.GreatGrandChild] is not found with the qualifiers 
Qualifiers:[@javax.inject.Named(value=greatGrandChild),@com.ibm.ws.fat.jcdi.inheritance.InheritedQualifier(value=override)]
for injection into Field Injection Point, field name :  greatGrandChild, Bean Owner : [MyBean, Name:myBean, WebBeans Type:MANAGED, API Types:[com.ibm.ws.fat.jcdi.inheritance.MyBean,java.lang.Object], Qualifiers:[javax.enterprise.inject.Default,javax.enterprise.inject.Any,javax.inject.Named]]
at org.apache.webbeans.util.InjectionExceptionUtils.throwUnsatisfiedResolutionException(InjectionExceptionUtils.java:77)
at org.apache.webbeans.container.InjectionResolver.checkInjectionPoints(InjectionResolver.java:193)
at org.apache.webbeans.container.BeanManagerImpl.validate(BeanManagerImpl.java:1031)
at org.apache.webbeans.config.BeansDeployer.validate(BeansDeployer.java:444)
at org.apache.webbeans.config.BeansDeployer.validateInjectionPoints(BeansDeployer.java:383)
at org.apache.webbeans.config.BeansDeployer.deploy(BeansDeployer.java:184)
at org.apache.webbeans.lifecycle.AbstractLifeCycle.startApplication(AbstractLifeCycle.java:155)
at org.apache.webbeans.web.lifecycle.WebContainerLifecycle.startApplication(WebContainerLifecycle.java:97)
at com.ibm.ws.webbeans.WebBeansInitializer.onStartup(WebBeansInitializer.java:70)
at com.ibm.ws.webcontainer.webapp.WebApp.initializeServletContainerInitializers(WebApp.java:2485)
at com.ibm.ws.webcontainer.webapp.WebApp.initialize(WebApp.java:1038)
at com.ibm.ws.webcontainer.webapp.WebApp.initialize(WebApp.java:6404)
at com.ibm.ws.webcontainer.osgi.DynamicVirtualHost.startWebApp(DynamicVirtualHost.java:446)
at com.ibm.ws.webcontainer.osgi.DynamicVirtualHost.createRunnableHandler(DynamicVirtualHost.java:248)
at com.ibm.ws.webcontainer.osgi.DynamicVirtualHost.createRunnableHandler(DynamicVirtualHost.java:313)
at com.ibm.ws.http.internal.VirtualHostImpl.discriminate(VirtualHostImpl.java:251)
at com.ibm.ws.http.dispatcher.internal.channel.HttpDispatcherLink.ready(HttpDispatcherLink.java:306)
at com.ibm.ws.http.channel.internal.inbound.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:449)
at com.ibm.ws.http.channel.internal.inbound.HttpInboundLink.handleNewRequest(HttpInboundLink.java:383)
at com.ibm.ws.http.channel.internal.inbound.HttpInboundLink.processRequest(HttpInboundLink.java:283)
at com.ibm.ws.http.channel.internal.inbound.HttpInboundLink.ready(HttpInboundLink.java:254)
at com.ibm.ws.tcpchannel.internal.NewConnectionInitialReadCallback.sendToDiscriminators(NewConnectionInitialReadCallback.java:174)
at com.ibm.ws.tcpchannel.internal.NewConnectionInitialReadCallback.complete(NewConnectionInitialReadCallback.java:83)
at com.ibm.ws.tcpchannel.internal.WorkQueueManager.requestComplete(WorkQueueManager.java:504)
at com.ibm.ws.tcpchannel.internal.WorkQueueManager.attemptIO(WorkQueueManager.java:574)
at com.ibm.ws.tcpchannel.internal.WorkQueueManager.workerRun(WorkQueueManager.java:929)
at com.ibm.ws.tcpchannel.internal.WorkQueueManager$Worker.run(WorkQueueManager.java:1018)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:744)

其他三次注射工作正常。

我在IBM Websphere Liberty Profile上运行此应用程序。

有没有人知道为什么这段代码不能从Java7移植到Java8?

2 个答案:

答案 0 :(得分:0)

此链接Troubleshooting contexts and dependency injection

表明你有某种类路径问题。你已经说过,当你在java 7中运行时,应用程序运行正常,但是对于java 8,它没有。执行环境差异使用的JVM?例如,使用java 8运行程序所做的唯一更改是将相同的Liberty Profile指向不同的JVM?

如果不是这样,那么两个执行环境之间的区别很大,而不是两个不同的JVM。

但是,您在评论中提到唯一的变化是JAVA_HOME环境变量。因此,您可能只是遇到了一个可能的应用程序框架错误,或者是java本身的错误。

我现在的建议是:

如果它不能与java 8一起使用,请避免使用它。 Java 8是一种新的,它还不到一年。对于服务器使用,它是一个婴儿......顺便说一句,最新的Websphere规范只支持java 6或7 ...查看它:Websphere 8.5.5 requirements - java

java 7和java 8之间存在已知的不兼容性,也许您刚刚间接地触发了其中一个: incompatibilities between java 7 and 8- oracle doc

答案 1 :(得分:0)

事实证明这是应用服务器中的一个错误;它现在已被打补丁。