【发布时间】:2016-11-13 03:56:47
【问题描述】:
我想在 WildFly 10 上运行的 maven 动态 Web 项目中的 bean 中注入一个 Logger。
我在我的 POM 中添加了依赖项。 WildFly 中提供了范围:
<!-- Logger -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.2</version>
<scope>provided</scope>
</dependency>
我创建了一个生产者:
import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.inject.Singleton;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Singleton
public class LoggerProducer {
@Produces
Logger createLogger(final InjectionPoint ip) {
return LoggerFactory.getLogger(ip.getMember().getDeclaringClass());
}
}
然后我尝试注入 Logger:
@Inject
private Logger log;
此时我已经收到警告:
没有 bean 有资格注入到注入点 [JSR-346 §5.2.2]
当我尝试使用它时,我收到了这个错误:
Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type Logger with qualifiers @Default
at injection point [BackedAnnotatedField] @Inject private init.StartupBean.log
at init.StartupBean.log(StartupBean.java:0)
我错过了什么?我是不是忘记了什么?
【问题讨论】:
-
我没有注射经验,所以我没有回答,只是一个问题:DI-Mechanism 怎么知道它必须使用您的 LoggerProducer 作为 Logger? Logger 本身不是唯一的类名。
-
我在 glassfish 上没有遇到此类异常,但 glassfish 4.1.1 有其自身的一系列问题。
-
我遇到了同样的问题。然后认识到我使用了不同的 Logger 类。我们必须确保在注入点和生产者点导入正确的 Logger 类。
标签: java logging dependency-injection slf4j