我想看看 Weld CDI 容器如何与 JPMS 一起工作。所以我有以下配置。
我有我的命名模块和weld-se-shaded 作为自动模块。在我的模块中,我有
module my.module {
requires weld.se.shaded;
exports com.foo;
}
新班级
package com.foo;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
@ApplicationScoped
public class NewClass {
@Inject
private NewBean bean;
public void doIt() {
bean.doIt();
}
}
新豆类
package com.foo;
import javax.enterprise.context.Dependent;
@Dependent
public class NewBean {
public void doIt() {
System.out.println("I am doing it");
}
}
这就是结果
Sep 18, 2017 2:33:12 PM org.jboss.weld.bootstrap.WeldStartup <clinit>
INFO: WELD-000900: 3.0.2 (2017-09-18 08:49)
Sep 18, 2017 2:33:12 PM org.jboss.weld.environment.deployment.discovery.ReflectionDiscoveryStrategy processAnnotatedDiscovery
INFO: WELD-ENV-000014: Falling back to Java Reflection for bean-discovery-mode="annotated" discovery. Add org.jboss:jandex to the classpath to speed-up startup.
Sep 18, 2017 2:33:12 PM org.jboss.weld.bootstrap.WeldStartup startContainer
INFO: WELD-000101: Transactional services not available. Injection of @Inject UserTransaction not available. Transactional observers will be invoked synchronously.
Exception in thread "main" java.lang.reflect.InaccessibleObjectException
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:488)
at java.base/java.util.concurrent.ForkJoinTask.getThrowableException(ForkJoinTask.java:590)
at java.base/java.util.concurrent.ForkJoinTask.get(ForkJoinTask.java:996)
at weld.se.shaded@3.0.2-SNAPSHOT/org.jboss.weld.executor.AbstractExecutorServices.checkForExceptions(AbstractExecutorServices.java:72)
at weld.se.shaded@3.0.2-SNAPSHOT/org.jboss.weld.executor.AbstractExecutorServices.invokeAllAndCheckForExceptions(AbstractExecutorServices.java:58)
at weld.se.shaded@3.0.2-SNAPSHOT/org.jboss.weld.executor.AbstractExecutorServices.invokeAllAndCheckForExceptions(AbstractExecutorServices.java:66)
at weld.se.shaded@3.0.2-SNAPSHOT/org.jboss.weld.bootstrap.ConcurrentBeanDeployer.createClassBeans(ConcurrentBeanDeployer.java:65)
at weld.se.shaded@3.0.2-SNAPSHOT/org.jboss.weld.bootstrap.BeanDeployment.createBeans(BeanDeployment.java:256)
at weld.se.shaded@3.0.2-SNAPSHOT/org.jboss.weld.bootstrap.WeldStartup.deployBeans(WeldStartup.java:422)
at weld.se.shaded@3.0.2-SNAPSHOT/org.jboss.weld.bootstrap.WeldBootstrap.deployBeans(WeldBootstrap.java:83)
at weld.se.shaded@3.0.2-SNAPSHOT/org.jboss.weld.environment.se.Weld.initialize(Weld.java:789)
at my.module/com.foo.NewMain.main(NewMain.java:47)
Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make field private com.foo.NewBean com.foo.NewClass.bean accessible: module my.module does not "opens com.foo" to module weld.se.shaded
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:337)
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:281)
at java.base/java.lang.reflect.Field.checkCanSetAccessible(Field.java:176)
at java.base/java.lang.reflect.Field.setAccessible(Field.java:170)
at weld.se.shaded@3.0.2-SNAPSHOT/org.jboss.weld.security.GetAccessibleCopyOfMember.of(GetAccessibleCopyOfMember.java:38)
at weld.se.shaded@3.0.2-SNAPSHOT/org.jboss.weld.security.GetAccessibleCopyOfMember.run(GetAccessibleCopyOfMember.java:44)
at weld.se.shaded@3.0.2-SNAPSHOT/org.jboss.weld.security.GetAccessibleCopyOfMember.run(GetAccessibleCopyOfMember.java:26)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at weld.se.shaded@3.0.2-SNAPSHOT/org.jboss.weld.injection.FieldInjectionPoint.<init>(FieldInjectionPoint.java:65)
如何解释这个异常
原因:java.lang.reflect.InaccessibleObjectException:无法使字段私有 com.foo.NewBean com.foo.NewClass.bean 可访问:模块 my.module 没有“打开 com.foo”到模块weld.se.shaded
这里 有一张表 ,我可以从中了解到自动模块可以读取命名(应用程序)模块。那为什么不呢?