简体   繁体   English

JSF 2.2和Spring 4和CDI在不同的层上而不会丢失Spring功能

[英]JSF 2.2 and Spring 4 and CDI on different layers without losing Spring features

I'm a little confused after all articles that I read. 阅读完所有文章后,我有些困惑。 I don't want to use additional services as I'll need if I use Weld Framework instead of simple Spring qualifier in service layer. 如果我在服务层中使用Weld Framework而不是简单的Spring限定符,则不需要使用其他服务。 And I have one service which uses JavaMailSender. 我有一项使用JavaMailSender的服务。 I just want to have ability use AOP in controller layer with JSF. 我只想拥有在JSF控制器层中使用AOP的能力。

I got absolutely confused after reading about support JSR-229 and JSR-330 by Spring (even Spring 3) 在阅读Spring(甚至Spring 3)的关于支持JSR-229和JSR-330的文章后,我感到非常困惑

Spring 3 And JSR-330 @Inject And @Named Example Spring 3和JSR-330 @Inject And @Named示例

Does it mean that I can do smth like that and don't lose posibility use Spring features like AOP ? 这是否意味着我可以那样做并且不会失去使用AOP之类的Spring功能的可能性? (Yes, I think.) (是的,我认为。)

import javax.inject.Inject;
import javax.inject.Named;
import java.io.Serializable;

@Named("newClientController")
@ViewScoped
public class NewClientController implements Serializable {
@Inject
private ClientService clientService;

////......
}

@Service
@Transactional
public class ClientService {

public ClientService(){
int i = 0;
}

@Autowired
@Qualifier("clientDAOMyBatis")
private ClientDAO clientDAO;
//....
}

I researched several days and found several groups of decisions 我研究了几天,发现了几组决策

1) Bridge between Spring and CDI 1) Spring和CDI之间的桥梁

https://stackoverflow.com/questions/5510144/cdi-bean-accessing-spring-beans Injecting a Spring bean using CDI @Inject https://stackoverflow.com/questions/5510144/cdi-bean-accessing-spring-beans 使用CDI @Inject注入Spring bean

Prons: 优点:

  1. Simple decision 简单的决定
  2. Don't lose Spring features 不要失去Spring功能

Cons: 缺点:

  1. They have doubtful reputation. 他们的声誉令人怀疑。 (bugs !!!) (臭虫!!!)

2) Use Spring @Component and create custom ViewScope for JSF http://blog.primefaces.org/?p=702 2)使用Spring @Component并为JSF创建自定义ViewScope http://blog.primefaces.org/?p=702

Prons: 优点:

  1. Simple decision 简单的决定
  2. Don't lose Spring features 不要失去Spring功能

Cons: 缺点:

  1. Absent implementation of support for destruction callbacks. 缺少对销毁回调的支持实现。

3) Serializable Spring Bean (smth strange) 3)可序列化的Spring Bean(有点奇怪)

https://codereview.stackexchange.com/questions/23790/spring-autowiring-in-managed-beans-with-support-for-serialization-is-this-safe https://codereview.stackexchange.com/questions/23790/spring-autowiring-in-managed-beans-with-support-for-serialization-is-this-safe

Please, don't close this questions. 请不要关闭这个问题。 I know that many related questions exist. 我知道存在许多相关问题。 Could you advice me some solution for that problem? 您能为我提供一些解决该问题的方法吗?

PS I use MyBatis which doesn't support JPA and Spring Java-based Configuration because I want to deploy this app in cloud. PS我使用MyBatis,它不支持JPA和基于Spring Java的配置,因为我想将此应用程序部署在云中。

I think to use Spring as CDI for JSF is best way (second option in your list) if you want all Spring features on JSF Managed Beans. 如果您希望JSF托管Bean上具有所有Spring功能,我认为将Spring用作JSF的CDI是最好的方法(列表中的第二个选择)。 to create custom ViewScope you can try this 要创建自定义ViewScope,您可以尝试以下操作

http://blog.harezmi.com.tr/uncategorized/spring-view-scope-for-jsf-2-users/ http://blog.harezmi.com.tr/uncategorized/spring-view-scope-for-jsf-2-users/

this is better implementation to use ViewScope with Spring. 这是将ViewScope与Spring一起使用的更好的实现。

-- UPDATE -- -更新-

I added a sample project on github. 我在github上添加了一个示例项目。 You can see it. 你可以看到。

https://github.com/bhdrk/Tutorials/tree/master/spring4-jsf22-integration https://github.com/bhdrk/Tutorials/tree/master/spring4-jsf22-integration

One of my blog's readers reports that Spring beans are serializable in the latest version of Spring 3.2 (and hopefully Spring 4, but he didn't say anything about it). 我的一位博客读者报告说,Spring bean在最新版本的Spring 3.2(以及希望是Spring 4)中可序列化。 So you can use a standard JSF 2.2 @ViewScoped controller and have your Spring beans injected as managed properties. 因此,您可以使用标准的JSF 2.2 @ViewScoped控制器,并将Spring Bean作为托管属性注入。 That's a tad unusual, because that's a third annotation (@ManagedProperty instead of @Autowire or @Inject). 这有点不寻常,因为这是第三个注释(@ManagedProperty而不是@Autowire或@Inject)。 But I'm reported it works fine. 但据报道,它运行良好。

Read the full story (it's a bit too long to copy it here) at my blog . 我的博客上阅读完整的故事(在这里复制它太长了)。

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

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