0

我正在为一堆接口创建一个后端服务实现,并尽可能拥有最干净的代码,我决定转向基于构造函数的 DI 而不是基于字段,但这让我很头疼。

使用 WebLogic 12.1.3,此类被正确实例化:

@Stateless(name = "MenuService")
@Slf4j
public class MenuFacade implements MenuService {

    @Inject
    private MenuRepository menuRepository;

    @Inject
    private MenuMapper menuMapper;

    public MenuFacade(MenuRepository menuRepository, MenuMapper menuMapper) {
        this.menuRepository = menuRepository;
        this.menuMapper = menuMapper;
    }

    public MenuFacade() {
    }

    @Override
    @Transactional
    public List<MenuDto> getMenuList() {

        log.trace("Fetching Menu entities from database");
        List<MenuEntity> menuEntities = menuRepository.findAll();
        log.trace("Menu entities: {}", menuEntities);

        return menuEntities.stream().map(menuMapper::menuEntityToMenuDto).collect(Collectors.toList());
    }
}

但是,当我尝试像这样重构它时:

@Stateless(name = "MenuService")
@Slf4j
public class MenuFacade implements MenuService {

    private MenuRepository menuRepository;

    private MenuMapper menuMapper;

    @Inject
    public MenuFacade(MenuRepository menuRepository, MenuMapper menuMapper) {
        this.menuRepository = menuRepository;
        this.menuMapper = menuMapper;
    }

    public MenuFacade() {
    }

    @Override
    @Transactional
    public List<MenuDto> getMenuList() {

        log.trace("Fetching Menu entities from database");
        List<MenuEntity> menuEntities = menuRepository.findAll();
        log.trace("Menu entities: {}", menuEntities);

        return menuEntities.stream().map(menuMapper::menuEntityToMenuDto).collect(Collectors.toList());
    }
}

我在部署期间遇到异常说:

####<Nov 7, 2018 5:17:36,164 PM GMT> <Error> <Deployer> <6738a70423ff> <backend> <[ACTIVE] ExecuteThread: '14' for queue: 'weblogic.kernel.Default (self-tuning)'> <<WLS Kernel>> <> <18022f2f-7d83-46d9-8868-1d1253f0571a-00000032> <1541611
056164> <[severity-value: 8] [rid: 0] [partition-id: 0] [partition-name: DOMAIN] > <BEA-149265> <Failure occurred in the execution of deployment request with ID "2093877349563649" for task "106" on [partition-name: DOMAIN]. Error is: "we
blogic.management.DeploymentException: CDI deployment failure:null"
weblogic.management.DeploymentException: CDI deployment failure:null
        at com.oracle.injection.integration.CDIAppDeploymentExtension.initCdi(CDIAppDeploymentExtension.java:95)
        at com.oracle.injection.integration.CDIAppDeploymentExtension.activate(CDIAppDeploymentExtension.java:43)
        at weblogic.application.internal.flow.AppDeploymentExtensionFlow.activate(AppDeploymentExtensionFlow.java:39)
        at weblogic.application.internal.BaseDeployment$2.next(BaseDeployment.java:752)
        at weblogic.application.utils.StateMachineDriver.nextState(StateMachineDriver.java:45)
        at weblogic.application.internal.BaseDeployment.activate(BaseDeployment.java:262)
        at weblogic.application.internal.EarDeployment.activate(EarDeployment.java:66)
        at weblogic.application.internal.DeploymentStateChecker.activate(DeploymentStateChecker.java:165)
        at weblogic.deploy.internal.targetserver.AppContainerInvoker.activate(AppContainerInvoker.java:90)
        at weblogic.deploy.internal.targetserver.operations.AbstractOperation.activate(AbstractOperation.java:631)
        at weblogic.deploy.internal.targetserver.operations.ActivateOperation.activateDeployment(ActivateOperation.java:171)
        at weblogic.deploy.internal.targetserver.operations.ActivateOperation.doCommit(ActivateOperation.java:121)
        at weblogic.deploy.internal.targetserver.operations.AbstractOperation.commit(AbstractOperation.java:348)
        at weblogic.deploy.internal.targetserver.DeploymentManager.handleDeploymentCommit(DeploymentManager.java:907)
        at weblogic.deploy.internal.targetserver.DeploymentManager.activateDeploymentList(DeploymentManager.java:1468)
        at weblogic.deploy.internal.targetserver.DeploymentManager.handleCommit(DeploymentManager.java:459)
        at weblogic.deploy.internal.targetserver.DeploymentServiceDispatcher.commit(DeploymentServiceDispatcher.java:181)
        at weblogic.deploy.service.internal.targetserver.DeploymentReceiverCallbackDeliverer.doCommitCallback(DeploymentReceiverCallbackDeliverer.java:217)
        at weblogic.deploy.service.internal.targetserver.DeploymentReceiverCallbackDeliverer.access$100(DeploymentReceiverCallbackDeliverer.java:14)
        at weblogic.deploy.service.internal.targetserver.DeploymentReceiverCallbackDeliverer$2.run(DeploymentReceiverCallbackDeliverer.java:69)
        at weblogic.work.SelfTuningWorkManagerImpl$WorkAdapterImpl.run(SelfTuningWorkManagerImpl.java:670)
        at weblogic.invocation.ComponentInvocationContextManager._runAs(ComponentInvocationContextManager.java:352)
        at weblogic.invocation.ComponentInvocationContextManager.runAs(ComponentInvocationContextManager.java:337)
        at weblogic.work.LivePartitionUtility.doRunWorkUnderContext(LivePartitionUtility.java:57)
        at weblogic.work.PartitionUtility.runWorkUnderContext(PartitionUtility.java:41)
        at weblogic.work.SelfTuningWorkManagerImpl.runWorkUnderContext(SelfTuningWorkManagerImpl.java:644)
        at weblogic.work.ExecuteThread.execute(ExecuteThread.java:415)
        at weblogic.work.ExecuteThread.run(ExecuteThread.java:355)
Caused By: java.lang.NullPointerException
        at org.jboss.weld.injection.InjectionPointFactory.getParameterInjectionPoints(InjectionPointFactory.java:250)
        at org.jboss.weld.injection.AbstractCallableInjectionPoint.<init>(AbstractCallableInjectionPoint.java:52)
        at org.jboss.weld.injection.ConstructorInjectionPoint.<init>(ConstructorInjectionPoint.java:63)
        at org.jboss.weld.injection.InjectionPointFactory.createConstructorInjectionPoint(InjectionPointFactory.java:175)
        at org.jboss.weld.injection.producer.SubclassedComponentInstantiator.forSubclassedEjb(SubclassedComponentInstantiator.java:58)
        at org.jboss.weld.injection.producer.ejb.SessionBeanInjectionTarget.initInstantiator(SessionBeanInjectionTarget.java:86)
        at org.jboss.weld.injection.producer.BasicInjectionTarget.<init>(BasicInjectionTarget.java:98)
        at org.jboss.weld.injection.producer.BasicInjectionTarget.<init>(BasicInjectionTarget.java:82)
        at org.jboss.weld.injection.producer.BeanInjectionTarget.<init>(BeanInjectionTarget.java:58)
        at org.jboss.weld.injection.producer.ejb.SessionBeanInjectionTarget.<init>(SessionBeanInjectionTarget.java:67)
        at org.jboss.weld.injection.producer.ejb.SessionBeanInjectionTarget.of(SessionBeanInjectionTarget.java:61)
        at org.jboss.weld.manager.InjectionTargetFactoryImpl.chooseInjectionTarget(InjectionTargetFactoryImpl.java:105)
        at org.jboss.weld.manager.InjectionTargetFactoryImpl.createInjectionTarget(InjectionTargetFactoryImpl.java:86)
        at org.jboss.weld.bean.SessionBean.<init>(SessionBean.java:89)
        at org.jboss.weld.bean.SessionBean.of(SessionBean.java:77)
        at org.jboss.weld.bootstrap.AbstractBeanDeployer.createSessionBean(AbstractBeanDeployer.java:292)
        at org.jboss.weld.bootstrap.BeanDeployer.createClassBeans(BeanDeployer.java:202)
        at org.jboss.weld.bootstrap.BeanDeployment.createBeans(BeanDeployment.java:256)
        at org.jboss.weld.bootstrap.WeldStartup.deployBeans(WeldStartup.java:397)
        at org.jboss.weld.bootstrap.WeldBootstrap.deployBeans(WeldBootstrap.java:83)
        at com.oracle.injection.provider.weld.WeldInjectionContainer.deploy(WeldInjectionContainer.java:143)
        at com.oracle.injection.integration.CDIAppDeploymentExtension.initCdi(CDIAppDeploymentExtension.java:82)
        at com.oracle.injection.integration.CDIAppDeploymentExtension.activate(CDIAppDeploymentExtension.java:43)
        at weblogic.application.internal.flow.AppDeploymentExtensionFlow.activate(AppDeploymentExtensionFlow.java:39)
        at weblogic.application.internal.BaseDeployment$2.next(BaseDeployment.java:752)
        at weblogic.application.utils.StateMachineDriver.nextState(StateMachineDriver.java:45)
        at weblogic.application.internal.BaseDeployment.activate(BaseDeployment.java:262)
        at weblogic.application.internal.EarDeployment.activate(EarDeployment.java:66)
        at weblogic.application.internal.DeploymentStateChecker.activate(DeploymentStateChecker.java:165)
        at weblogic.deploy.internal.targetserver.AppContainerInvoker.activate(AppContainerInvoker.java:90)
        at weblogic.deploy.internal.targetserver.operations.AbstractOperation.activate(AbstractOperation.java:631)
        at weblogic.deploy.internal.targetserver.operations.ActivateOperation.activateDeployment(ActivateOperation.java:171)
        at weblogic.deploy.internal.targetserver.operations.ActivateOperation.doCommit(ActivateOperation.java:121)
        at weblogic.deploy.internal.targetserver.operations.AbstractOperation.commit(AbstractOperation.java:348)
        at weblogic.deploy.internal.targetserver.DeploymentManager.handleDeploymentCommit(DeploymentManager.java:907)
        at weblogic.deploy.internal.targetserver.DeploymentManager.activateDeploymentList(DeploymentManager.java:1468)
        at weblogic.deploy.internal.targetserver.DeploymentManager.handleCommit(DeploymentManager.java:459)
        at weblogic.deploy.internal.targetserver.DeploymentServiceDispatcher.commit(DeploymentServiceDispatcher.java:181)
        at weblogic.deploy.service.internal.targetserver.DeploymentReceiverCallbackDeliverer.doCommitCallback(DeploymentReceiverCallbackDeliverer.java:217)
        at weblogic.deploy.service.internal.targetserver.DeploymentReceiverCallbackDeliverer.access$100(DeploymentReceiverCallbackDeliverer.java:14)
        at weblogic.deploy.service.internal.targetserver.DeploymentReceiverCallbackDeliverer$2.run(DeploymentReceiverCallbackDeliverer.java:69)
        at weblogic.work.SelfTuningWorkManagerImpl$WorkAdapterImpl.run(SelfTuningWorkManagerImpl.java:670)
        at weblogic.invocation.ComponentInvocationContextManager._runAs(ComponentInvocationContextManager.java:352)
        at weblogic.invocation.ComponentInvocationContextManager.runAs(ComponentInvocationContextManager.java:337)
        at weblogic.work.LivePartitionUtility.doRunWorkUnderContext(LivePartitionUtility.java:57)
        at weblogic.work.PartitionUtility.runWorkUnderContext(PartitionUtility.java:41)
        at weblogic.work.SelfTuningWorkManagerImpl.runWorkUnderContext(SelfTuningWorkManagerImpl.java:644)
        at weblogic.work.ExecuteThread.execute(ExecuteThread.java:415)
        at weblogic.work.ExecuteThread.run(ExecuteThread.java:355)

它是 WebLogic 的错误吗?为什么一个工作,而另一个不工作?

4

1 回答 1

0

从这个链接http://docs.jboss.org/weld/reference/latest/en-US/html_single/#_injection_points

依赖注入总是在容器第一次实例化 bean 实例时发生。稍微简化一下,事情按以下顺序发生:

首先,容器调用 bean 构造函数(默认构造函数或注解 @Inject 的构造函数),以获取 bean 的实例。接下来,容器初始化 bean 的所有注入字段的值。接下来容器调用bean的所有初始化方法(调用顺序不可移植,不要依赖)。最后,@PostConstruct调用该方法(如果有)。

所以看起来如果你有构造函数注入,你必须删除无参数构造函数。

于 2018-11-07T20:02:09.453 回答