Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jsf 2.0 spring 3 scope request not working

Tags:

spring

jsf-2

I am trying to use spring to provide managed beans to jsf. I assume that @ManagedBean will be picked up by JSF container to link the EL in JSF to managed bean even when I use spring by configuring spring usage in faces-config.xml.

Spring shall provide the beans but now who manages scope of the beans?

I have tried following annotation on beans to have it become Request scope but they do not work.

@ManagedBean(name="helloBean") //meant for JSF
@RequestScoped //meant for JSF
@Scope(value="request") //meant for spring
@Controller //meant for spring
public class HelloBean implements Serializable {

Actually earlier I was using plain JSF and @ManagedBean and @RequestScoped were working well. Now as I tried to integrate using spring the scope are not working.

I have even tried setting bean scope in spring config but they work as expected in context of spring (singleton and prototype) but not web request context.

I was trying to avoid having to use above @Scope and @Controller annotation hoping that JSF will manage scope but do not seem like.

Below are my files snippet for spring config and MyHelloBean which probably will help communicate better.

<bean id="helloBean" class="com.mkyong.common.HelloBean" init-method="init" />

<bean id="myHelloBean" class="com.mkyong.common.MyHelloBean" init-method="init" >
        <property name="helloBean" ref="helloBean"></property>
</bean>

@ManagedBean
@RequestScoped
@Scope(value="request")
@Controller
public class MyHelloBean implements Serializable {

    private static final long serialVersionUID = 1L;
    //@ManagedProperty(value = "#{helloBean}")
    private HelloBean helloBean;

see in above MyHelloBean I am using spring DI to set helloBean which gets set by spring fine. I have commented out @ManagedBean which I think I can leave it in there as it will be ignored by spring any ways and JSF is not going to process it I guess but to be safe I commented it out for JSF to not process it.

To complete I use below in faces-config to activate spring usage.

<el-resolver> 
org.springframework.web.jsf.el.SpringBeanFacesELResolver 
</el-resolver> 

Regards,

Miten.

like image 685
Miten Avatar asked May 31 '12 07:05

Miten


1 Answers

Our team faced similar problems integrating JSF and Spring beans, including problems with their scopes. And here I am to share our knowledge.

Scopes

Basically now, when you defined in your application context, that Spring will be managing your beans, thus scopes. Spring will map JSF scope annotations to his native scope annotations.

  • Example when both Spring and JSF support provided scope:

@RequestScoped annotation will be mapped to @Scope("request") Spring's annotation, etc with other supported scopes.

  • Example when Spring does not support JSF's provided scope:

@ViewScoped is not defined in Spring's native scope annotations, thus (not sure) it will use Spring's default scope, which is singleton, or request scope (not sure).

Bean Injection

In JSF2 you used @ManagedProperty annotations for injection, while Spring uses @Autowired annotation. What are the differences and which to choose?

  • Injecting Spring beans with @ManagedProperty:

Spring component you wish to inject must have a value which will match jsf injection annotation's value: @Component(value = "valueMatches") injected with @ManagedProperty(value = "valueMatches").

  • Injecting Spring beans with @Autowired:

Spring component you wish to inject must does not require a custom value to distinguish, if it is the only implementation of the bean you are injecting: @Component injected with @Autowired.

Our way

We used Spring's annotations for defining Beans, Scopes and Injection.

We marked JSF beans with @Scope(value = "desiredScope"), @Controller(value = "beanName") and @Qualifier(value = "beanName") annotations. Later which could be accessed from JSF context with help of in faces-config.xml via "beanName" value defined in the @Controller annotation.

We marked Spring services with @Service annotation.

We injected Spring services and JSF beans with @Autowired annotation.

We found ViewScope and FlashScope custom implemetations on the web and used them for our beans. Thus we did not lose any of JSF2 scopes and even added new one.

Hope this helps.

like image 67
JMelnik Avatar answered Nov 04 '22 07:11

JMelnik