tweetysat
tweetysat

Reputation: 2403

CDI producer benefit

I'm trying to convert jsf2 application from managed beans to CDI.

So I'm learning and discovering the advantages of CDI. It seems great but there is something I don't understand with producer.

About CDI I read that one of the advantages is to reduce coupling. I did some experimentation with an interface, some implementation, the @default, @alternate, @inject. And it's doing the job. In that case I can see the coupling 'reduction'.

But when implementing a producer for logging, I don't see it.

For now I'm using something like

Logger log = LoggerFactory.getLogger(MyClass.class);

With CDI, after creating the producer, I have

@Inject
Logger logger;

Ok, it's shorter but the Logger class is still coupled with e.g. log4j.

So if I want to use another logger library I'll still have to modify all my classes.

Is there something I didn't understand ?

Upvotes: 0

Views: 69

Answers (1)

Leo G.
Leo G.

Reputation: 458

You're idea is not bad, and it can indeed helps to abstract which logger you use. However, you may need to use a logging adapter such as slf4j (https://www.slf4j.org/) or commons-logging, that will help you to choose between different loggers implementations (log4j,etc.) but keeping the same logging interface.

You then keep a single producer method in CDI that can be customize at will.

Upvotes: 2

Related Questions