英文:
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 <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.
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;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论