Quarkus / CDI and “java config” DI definitions

断了今生、忘了曾经 提交于 2020-01-16 01:46:16

问题


I just started a quarkus proof of concept. The containers-start time is amazing!

Right now, I'm working on the Dependency Injection part. And figuring out the options.

https://quarkus.io/blog/quarkus-dependency-injection/

My preferences are:

I prefer constructor injection. (This has been going ok).

I prefer "java config" so I can follow the "Composition Root" pattern of putting all my application dependency injections in a common place. (See https://blog.ploeh.dk/2011/07/28/CompositionRoot/ )

With Spring DI, this is done with the

org.springframework.context.annotation.Configuration

and declaring the Beans there.

Aka, I prefer not to place "@ApplicationScoped" annotations all over my classes.

Does CDI/Quarkus support a "java config" model? The reason I ask about quarkus is that I read quarkus has a limited CDI implementation.

//start quote//Our primary goal was to implement a supersonic build-time oriented DI solution compatible with CDI. This would allow users to continue using CDI in their applications but also leverage Quarkus build-time optimizations. However, ArC is not a full CDI implementation verified by the TCK - see also the list of supported features and the list of limitations.//end quote

So my question isn't a solely CDI question.

I've tried different internet search terms, but they keep showing me Spring links. :(


回答1:


You should create a CDI bean that will produce your beans, this is the standard CDI approach to what Spring calls Java Configuration.

So something like this

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;


@ApplicationScoped
public class MyConfiguration {
    @Produces
    public MyBean myBean(){
        return new MyBean();
    }
}


来源:https://stackoverflow.com/questions/58544079/quarkus-cdi-and-java-config-di-definitions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!