简体   繁体   中英

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 eg 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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM