@Singleton provides not only transaction, but also default thread safety. Therefore, if you replace it with @ApplicationScoped , you will lose synchronization. Therefore, to do this correctly, you need to do the following:
@ApplicationScoped public class LoggerProducer { private final ConcurrentMap<String, Logger> loggers = new ConcurrentHashMap<>(); @Produces public Logger getProducer(InjectionPoint ip) { String key = getKeyFromIp(ip); loggers.putIfAbsent(key, Logger.getLogger(key)); return loggers.get(key); } private String getKeyFromIp(InjectionPoint ip) { return ip.getMember().getDeclaringClass().getCanonicalName(); } }
You can also do this completely without any area if you make the map static
source share