我很难让注射在一个独立的焊接容器中运行。它是在库代码中运行的,它也在wildfly容器上运行。其他注射在焊接容器中工作,只是这一个给我带来了麻烦。我已经尝试过通过SeContainerInitializer专门添加类和接口。我还尝试过创建一个工厂并使用produces方法。
类上的注释(它是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;焊接自举
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实例上,我对它没有问题。部署过程中没有错误。
我所有的jars中都有一个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。java版本"1.8.0_121“AbstractConnection和ExecutorImpl位于相同的包和jar文件中。RITExecutor接口在另一个jar文件中。
我已经升级到weld 3.0.3,并且在行为上尝试了applicationScope注解。
发布于 2018-04-09 20:12:16
这是我的测试场景。PostConstruct在选择bean时工作得很好。
```javascript打包org.dpp.cdi.singleton;
导入java.util.logging.Logger;
导入javax.annotation.PostConstruct;
导入javax.enterprise.inject.se.SeContainer;
导入javax.enterprise.inject.se.SeContainerInitializer;
导入javax.inject.Singleton;
导入org.jboss.weld.environment.se.Weld;
/**
*
*/
公共类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();}@Singletonpublic 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>` 问题可能出在焊接版本上。
https://stackoverflow.com/questions/49683068
复制相似问题