3

我想看看 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

这里 有一张表在此处输入图像描述 ,我可以从中了解到自动模块可以读取命名(应用程序)模块。那为什么不呢?

4

1 回答 1

3

您可能应该尝试更换

exports com.foo

在你module-info.java

opens com.foo 

模块声明中的opens指令声明一个包是开放的,以允许包中的所有类型及其所有成员,而不仅仅是公共类型及其公共成员,由支持私有访问或绕过或抑制方法的 API 反映默认 Java 语言访问控制检查。

于 2017-09-18T15:13:04.923 回答