我试图让CDI在JBeret SE上发挥作用。这是我的密码:
SampleBatchlet类
@Named
public class SampleBatchlet extends AbstractBatchlet
{
@Inject
@BatchProperty(name = "foo")
String foo;
@Inject
StepContext stepContext;
@Inject
Logger logger;
@Override
public String process() throws Exception {
final String say = stepContext.getProperties().getProperty("say");
System.out.println("hello foolish");
return null;
}
}SampleBatchletTest类
@EnableWeld
class SampleBatchletTest {
@Inject
Logger logger;
@WeldSetup
public WeldInitiator weld = WeldInitiator
.from(
LoggerProducer.class
)
.activate(
RequestScoped.class,
ApplicationScoped.class
)
.build();
@Test
void app() throws InterruptedException {
final JobOperator jobOperator = BatchRuntime.getJobOperator();
long id = jobOperator.start("simplebatchlet", null);
final JobExecutionImpl jobExecution = (JobExecutionImpl) jobOperator.getJobExecution(id);
jobExecution.awaitTermination(5, TimeUnit.SECONDS);
Assertions.assertEquals(BatchStatus.COMPLETED, jobExecution.getBatchStatus());
}
}服务器类
@ApplicationScoped
public class Server {
@Inject
private Logger logger;
public void init(@Observes @Initialized(ApplicationScoped.class) Object init) throws InterruptedException {
logger.info("init");
}LoggerProducer类
public class LoggerProducer {
@Produces
public Logger produceLogger(InjectionPoint injectionPoint) {
return LoggerFactory.getLogger(injectionPoint.getMember().getDeclaringClass().getName());
}
}问题是,在SampleBatchlet上没有注入Logger实例,而在上面的测试和服务器类中都正确地注入了Logger实例。
有什么暗示吗?
小更新
通过阅读这个参考资料
我发现java.util.logging.Logger可以注射。
所以我增加了
<batchlet ref="it.infocert.shop.main.SampleBatchlet" >
<properties>
<property name="logger" value="java.util.logging.Logger" />
</properties>
</batchlet>价值实际上可以是任何东西。
在SampleBatchlet上我添加了
@Inject
@BatchProperty
Logger logger;现在它被注入了。我有点困惑,因为我希望使用另一个记录器实现..
发布于 2019-11-29 16:58:42
当通过@BatchProperty进行注入时,JBeret尝试检查注入字段的类型并将其与注入值匹配,并实例化一个实例以供注入。这就是为什么注入由JBeret创建的记录器,而不是您自己的记录器。有关详细信息,请参阅JBeret BatchBeanProducer.java
要通过生产者方法注入您自己的记录器,您可能需要添加一个限定符来消除它。例如,
public class LoggerProducer {
@Produces
@Named("myLogger")
public Logger produceLogger(InjectionPoint injectionPoint) {
return LoggerFactory.getLogger(injectionPoint.getMember().getDeclaringClass().getName());
}
}
@Inject
@Named("myLogger")
Logger logger;发布于 2019-12-02 10:30:22
我从以下位置更改了xml上的批处理引用:
<batchlet ref="it.infocert.shop.main.SampleBatchlet">至:
<batchlet ref="sampleBatchlet">现在起作用了
https://stackoverflow.com/questions/59107422
复制相似问题