1

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. And I have one service which uses JavaMailSender. I just want to have ability use AOP in controller layer with JSF.

I got absolutely confused after reading about support JSR-229 and JSR-330 by Spring (even Spring 3)

Spring 3 And JSR-330 @Inject And @Named Example

Does it mean that I can do smth like that and don't lose posibility use Spring features like AOP ? (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

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

Prons:

  1. Simple decision
  2. Don't lose Spring features

Cons:

  1. They have doubtful reputation. (bugs !!!)

2) Use Spring @Component and create custom ViewScope for JSF http://blog.primefaces.org/?p=702

Prons:

  1. Simple decision
  2. Don't lose Spring features

Cons:

  1. Absent implementation of support for destruction callbacks.

3) Serializable Spring Bean (smth strange)

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?

P.S. I use MyBatis which doesn't support JPA and Spring Java-based Configuration because I want to deploy this app in cloud.

Community
  • 1
  • 1
Ray
  • 1,788
  • 7
  • 55
  • 92

2 Answers2

4

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. to create custom ViewScope you can try this

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

this is better implementation to use ViewScope with Spring.

-- UPDATE --

I added a sample project on github. You can see it.

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

Håken Lid
  • 22,318
  • 9
  • 52
  • 67
bhdrk
  • 3,415
  • 26
  • 20
  • I have just one question. Why did you create own annotatopns for spring scopes? – Ray May 01 '14 at 18:42
  • it's just a choice. you can use either @Scope("Name") or your own annotations. I prefer my own annotation because it's more IDE friendly. e.g. you can easily find usage of request scopes or application scopes. – bhdrk May 01 '14 at 19:18
  • Do you have any ideas for replacement Singleton and Inject here https://github.com/svn2github/primefaces-showcase/blob/master/src/main/java/org/primefaces/examples/push/chat/ChatUsers.java ? – Ray May 01 '14 at 19:49
  • Sorry, I posted wrong link. The right: https://github.com/svn2github/primefaces-showcase/blob/master/src/main/java/org/primefaces/examples/push/chat/ChatResource.java No, now as I understand all beans are spring bean. But PrimePush is editional framework, and this bean uses Inject for ServletContext it's CDI but if I refuse from CDI. When I replaced annotation for ServletContext ctx I get null – Ray May 01 '14 at 21:15
  • which application server do you use to run your application? JBoss AS, Glassfish or another? – bhdrk May 02 '14 at 10:18
  • I use Tomcat with Weld or I can use JBoss – Ray May 03 '14 at 18:40
0

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). So you can use a standard JSF 2.2 @ViewScoped controller and have your Spring beans injected as managed properties. That's a tad unusual, because that's a third annotation (@ManagedProperty instead of @Autowire or @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.

Stephan Rauh
  • 3,069
  • 2
  • 18
  • 37