【发布时间】:2017-05-29 07:21:33
【问题描述】:
我的 log4j 配置文件中有多个 RollingFileAppender 以管理不同的日志类型。
这是我的 LoggerFactory 类:
public class LoggerFactory {
static {
BasicConfigurator.configure();
}
@Produces
public Logger produceLog(InjectionPoint ip) {
return Logger.getLogger(ip.getMember().getDeclaringClass().getName());
}
@Produces
@LogType(loggerType = LogType.LoggerType.CRITICAL)
public Logger produceCriticalLogger(InjectionPoint injectionPoint) {
return Logger.getLogger("critical");
}
@Produces
@LogType(loggerType = LogType.LoggerType.SERVICE_TIMING)
public Logger produceServiceTimingLogger(InjectionPoint injectionPoint) {
return Logger.getLogger("serviceTiming");
}
@Produces
@LogType(loggerType = LogType.LoggerType.HEALTH)
public Logger produceHealthLogger(InjectionPoint injectionPoint) {
return Logger.getLogger("health");
}
@Produces
@LogType(loggerType = LogType.LoggerType.HTTP_HEADER)
public Logger produceHttpLogger(InjectionPoint injectionPoint) {
return Logger.getLogger("httpHeader");
}
@Produces
@LogType(loggerType = LogType.LoggerType.POSTED_REQUEST)
public Logger producePostRequestLogger(InjectionPoint injectionPoint) {
System.out.println(" ********* getAnnotated" + injectionPoint.getAnnotated());
System.out.println(injectionPoint.toString());
return Logger.getLogger("postedRequest");
}
@Produces
@LogType(loggerType = LogType.LoggerType.DB_TIMING)
public Logger produceDbTimingLogger(InjectionPoint injectionPoint) {
return Logger.getLogger("dbTiming");
}
@Produces
@LogType(loggerType = LogType.LoggerType.STACK_TRACE)
public Logger produceStackTraceLogger(InjectionPoint injectionPoint) {
return Logger.getLogger("stackTrace");
}
@Produces
@LogType(loggerType = LogType.LoggerType.TRANSACTIONAL)
public Logger produceTransactionsLogger(InjectionPoint injectionPoint) {
return Logger.getLogger("transactions");
}
}
这是我的注释:
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({FIELD, METHOD, TYPE})
public @interface LogType {
LoggerType loggerType();
public enum LoggerType {
CRITICAL,
DB_TIMING,
HEALTH,
HTTP_HEADER,
POSTED_REQUEST,
SERVICE_TIMING,
STACK_TRACE,
TRANSACTIONAL
}
}
这是我的注入点
@Inject
@LogType(loggerType = LogType.LoggerType.HEALTH)
public transient Logger logger;
我收到此错误:
Unsatisfied dependencies for type Logger with qualifiers @LogType
at injection point [BackedAnnotatedField] @Inject @LogType public transient myclass.logger
其他信息 :
我的项目中有更多的注入点并且它们工作正常,我确信 WELD 库的健康状况......而且我在 IDE 中看到了将我链接到生产者方法的 bean 图标。但我仍然找不到问题!
问题:
使用 CDI 实现多个记录器类型的最佳实践是什么?
【问题讨论】:
-
您显示的代码均未定义或引用
@Loggers -
@steve-c ,从
@Loggers重构为@LogType -
为什么是你的应用程序的结构?它是一个简单的 WAR 还是一个 EAR 文件?
-
我的应用程序是一种企业“多模块maven项目”,所以它是EAR。当我将 beans.xml 文件从 EJB 模块移动到我的 Web 模块时,问题解决了。
标签: java jakarta-ee dependency-injection log4j cdi