简体   繁体   English

Spring 5.x 和 CDI 2.x 集成选项

[英]Spring 5.x and CDI 2.x Integration Options

Considering Spring's 5.x baseline and CDI's baseline 2.x, what more viable options should I consider to integrate them into a project with JSF 2.3, since JSF 2.3 is coupled with the CDI?考虑到 Spring 的 5.x 基线和 CDI 的 2.x 基线,由于 JSF 2.3 与 CDI 耦合,我应该考虑将它们集成到具有 JSF 2.3 的项目中的哪些更可行的选项? Bridges?桥梁? Custom Bean Factories?定制豆工厂? Others?其他的?

We use bean producers to access Spring objects in CDI.我们使用 bean 生产者来访问 CDI 中的 Spring 对象。 As in the architecture we used there is an interface layer between the UI and the server/business, this integration was facilitated.在我们使用的架构中,UI 和服务器/业务之间有一个接口层,这种集成很容易实现。 Integration is performed as follows.积分如下进行。

Cdi Factory from the view/ui layer.来自 view/ui 层的 Cdi Factory。

public class MainViewClientFactory {

    public MainViewClientFactory() {
    }

    @ApplicationScoped
    @Produces
    public CadastroPaisService cadastroPaisService() {
        return CdiSpringUtils.getSpringBean(CadastroPaisService.class);
    }

}

The CdiSpringUtils Class. CdiSpringUtils Class。

public class CdiSpringUtils {

    private CdiSpringUtils() {
    }

    public static <R, Q extends Annotation> R getSpringBean(Class<R> beanClass) {
        return ApplicationContextProvider.getApplicationContext().getBean(beanClass);
    }

    public static <R, Q extends Annotation> R getSpringBean(Class<R> beanClass, Class<Q> qualifierClass) {
        return ApplicationContextProvider.getQualifiedBeanOfType(beanClass, qualifierClass);
    }

}

The ApplicationContextProvider Class. ApplicationContextProvider Class。

public class ApplicationContextProvider implements ApplicationContextAware {

    private static ApplicationContext context;

    public static ApplicationContext getApplicationContext() {
        return context;
    }

    public static <R, Q extends Annotation> R getQualifiedBeanOfType(Class<R> cls, Class<Q> qualifierAnnotationClass) {
        R bean = null;
        Map<String, R> beanMap = getApplicationContext().getBeansOfType(cls);
        for (Map.Entry<String, R> entry : beanMap.entrySet()) {
            Q targetAnnotation = getApplicationContext().findAnnotationOnBean(entry.getKey(), qualifierAnnotationClass);
            if (targetAnnotation != null) {
                bean = entry.getValue();
                break;
            }
        }
        return bean;
    }

    @Override
    public void setApplicationContext(ApplicationContext ctx) {
        context = ctx;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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