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

huangapple go评论46阅读模式
英文:

Spring 5.x and CDI 2.x Integration Options

问题

考虑到Spring的5.x基线和CDI的基线2.x,我在将它们集成到一个带有JSF 2.3的项目中时,应该考虑哪些更可行的选项,因为JSF 2.3与CDI耦合在一起?桥接?自定义Bean工厂?其他?

英文:

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? Bridges? Custom Bean Factories? Others?

答案1

得分: 0

我们使用Bean生产者来访问CDI中的Spring对象。与我们使用的架构一样,在UI和服务器/业务之间有一个接口层,这种集成得以实现。
集成操作如下。

来自视图/UI层的Cdi工厂。

public class MainViewClientFactory {

    public MainViewClientFactory() {
    }

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

}

CdiSpringUtils类。

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);
    }

}

ApplicationContextProvider类。

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;
    }
}

英文:

We use bean producers to access Spring objects in CDI. As in the architecture we used there is an interface layer between the UI and the server/business, this integration was facilitated.
Integration is performed as follows.

Cdi Factory from the view/ui layer.

public class MainViewClientFactory {

    public MainViewClientFactory() {
    }

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

}

The CdiSpringUtils Class.

public class CdiSpringUtils {

    private CdiSpringUtils() {
    }

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

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

}

The ApplicationContextProvider Class.

public class ApplicationContextProvider implements ApplicationContextAware {

    private static ApplicationContext context;

    public static ApplicationContext getApplicationContext() {
        return context;
    }

    public static &lt;R, Q extends Annotation&gt; R getQualifiedBeanOfType(Class&lt;R&gt; cls, Class&lt;Q&gt; qualifierAnnotationClass) {
        R bean = null;
        Map&lt;String, R&gt; beanMap = getApplicationContext().getBeansOfType(cls);
        for (Map.Entry&lt;String, R&gt; 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;
    }
}

huangapple
  • 本文由 发表于 2020年7月30日 22:02:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63174862.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定