对wildfly起作用的注射不适用于焊接。其他注射同时适用于

时间:2018-04-05 23:33:30

标签: java cdi code-injection weld

我很难让注射在独立的焊接容器中运行。它在库代码中也可以在它工作的wildfly容器上运行。其他注入工作在焊接容器中,这只是给我带来麻烦。我试过通过SeContainerInitializer专门添加类和接口。我已经尝试创建工厂并使用生产方法。

类的注释(它是javax.inject.Singleton)。它有一个公共的无参数构造函数

package com.ticomgeo.ftc.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory; 
import javax.annotation.PostConstruct;
import javax.inject.Singleton;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

  @Singleton
  public class ExecutorImpl implements RITExecutor {
  private  static  ScheduledExecutorService TIMER = executors.newSingleThreadScheduledExecutor();
  private static final Logger LOG = LoggerFactory.getLogger(new Throwable().getStackTrace()[0].getClassName());

  public ExecutorImpl() { super(); }

  @PostConstruct
    void initImpl() {     
    LOG.info("======================================Initializedingleton");
  }

  @Override
  public  void dispose() {
    TIMER.shutdownNow();
  }

  @Override
  public boolean isDisposed() {
    return TIMER.isShutdown();
  }

  @Override
  public <J> ScheduledFuture<J> schedule(Callable<J> job, long delay, TimeUnit unit) {
    return TIMER.schedule(job, delay, unit);
  }

  @Override
  public ScheduledFuture<?> schedule(Runnable job, long delay, TimeUnit unit) {
    return TIMER.schedule(job, delay, unit);
  }

  @Override
  public <J> Future<J> submit(Callable<J> job) {
    return TIMER.submit(job);
  }

  @Override
  public  Future<?> submit(Runnable job) {
    return TIMER.submit(job);
  }

  @Override
  public  <J> Future<J> submit(Runnable job, J result) {
    return TIMER.submit(job, result);
  }
}

接口

package com.ticomgeo.ftc.util;
import javax.inject.Singleton;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

public interface RITExecutor {
    void dispose();

    boolean isDisposed();

    <J> ScheduledFuture<J> schedule(Callable<J> job, long delay, TimeUnit unit);

    ScheduledFuture<?> schedule(Runnable job, long delay, TimeUnit unit);

    <J> Future<J> submit(Callable<J> job);

    Future<?> submit(Runnable job);

    <J> Future<J> submit(Runnable job, J result);
}

注射

@Inject
RITExecutor executor;

Weld bootstrap

  public static void main(String[] args) {
    SeContainerInitializer initializer = SeContainerInitializer.newInstance();
    /* disable discovery and register classes manually */
    try (SeContainer container = initializer.disableDiscovery()
        .addPackages(FTCServer.class)
        .addPackages(RITServer.class)
        .addPackages(Config.class)
        .addBeanClasses(ExecutorImpl.class, RITExecutor.class)
        .addPackages(AbstractConnection.class)
        .initialize()) {
    container.select(RITServer.class);
}

当我尝试访问weld-se实例中的注入bean时,我得到一个空指针异常。我对wildfly实例没有任何问题。部署期间没有错误。

我的所有罐子里都有一个beans.xml文件,虽然我认为它是由SeContainerInitializer超级控制的。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee    http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
   bean-discovery-mode="all">
</beans>

我正在使用weld-se-core 3.0.2.Final。 java版&#34; 1.8.0_121&#34; AbstractConnection和ExecutorImpl位于同一个包和jar文件中。 RITExecutor接口在另一个jar文件中。

我已升级到焊接3.0.3.Final,并尝试使用applicationScope注释而没有机会。

1 个答案:

答案 0 :(得分:0)

这是我的测试场景。 PostConstruct在选择bean时工作正常。

```

package org.dpp.cdi.singleton;

import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.enterprise.inject.se.SeContainer;
import javax.enterprise.inject.se.SeContainerInitializer;
import javax.inject.Singleton;
import org.jboss.weld.environment.se.Weld;

/**
 *
 * @author mochieng
 */
public class TestMain {

    public static void main(String[] args) {
        SeContainerInitializer weld = Weld.newInstance();
        try (SeContainer se = weld.disableDiscovery().addBeanClasses(TestServiceImpl.class).initialize()) {
            TestService service = se.select(TestService.class).get();
        }
    }

    public static interface TestService {

        void doTest();
    }

    @Singleton
    public static class TestServiceImpl implements TestService {

        private static final Logger LOG = Logger.getLogger(TestServiceImpl.class.getName());

        @PostConstruct
        void initImpl() {
            LOG.info("Initialized singleton");
        }

        @Override
        public void doTest() {
            LOG.info("Singleton called");
        }
    }
}

```

和maven依赖:

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.dpp</groupId> <artifactId>cdi-singleton</artifactId> <version>1.0.0-SNAPSHOT</version> <packaging>jar</packaging> <dependencies> <dependency> <groupId>org.jboss.weld.se</groupId> <artifactId>weld-se-shaded</artifactId> <version>3.0.3.Final</version> </dependency> </dependencies> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> </project>

问题可能是你的焊接版本。