use javax.faces.view.ViewScoped with CDI Spring bean and JSF - spring

I'm using Spring 3.1 JSF 2.2.
Annoting Bean with ViewScoped introduced by JSF 2.2 not work.
#javax.inject.Named
#javax.faces.view.ViewScoped
public class TestBean {
#PostConstruct
public void init(){sysout("Why spring invoke this when initializing context :-( ");}
}
In my applicationContext.xml there is an annotation component-scan tag
<context:component-scan base-package="com.test"/>
Spring 3.1 detect and deal with CDI annotation but #javax.faces.view.ViewScoped not work. I know there is another solution by creating my own ViewScoped implementation but i want to know why #javax.faces.view.ViewScoped not work

Best solution was removing spring and using a Java EE implementation of CDI

You should notice that JSF annotations will nor work for Spring beans, because JSF beans located in different context.
But view scope implementation is pretty simple. I've created an artifact to solve this problem.
See my github javaplugs/spring-jsf repository.

javax.faces.view.ViewScoped will only work for JSF Managed Bean and not for CDI.
Use javax.faces.bean.ManagedBean annotation if you want to have a correct behavior with View scope instead of javax.inject.Named.
Regards

Related

SpringBeanFacesELResolver not working with spring WebFlow

I added SpringBeanFacesELResolver to my faces-config.xml but always when I am trying to inject property using #ManagedProperty the injection field is null.
Do you know if SpringBeanFacesELResolver works together with Spring WebFlow?
I had some problems injecting spring beans to manage beans with #ManagedProperty, i had also SpringBeanFacesELResolver the problem was that the beans have the #Transactional annotation and this beans has no interface when i created the interface and use the interface in the manage bean the injection was done, maybe this help you but i'm not using spring webflow
Note: I want to write this as comment but i don't know why i couldn't

How to inject service in JSF managed bean without using Spring IOC

Typically if I have to inject a service in Spring I use
<bean id="mycontroller" class="com.MyController">
<property name="myService" ref="myService" />
and
<bean id="myService" class="com.MyService"></bean>
How to do the same when using JSF? I dont want to use two IOC containers for the beans and rather keep it in faces context itself. I have seen links such as
JSF 2 inject Spring bean/service with #ManagedProperty and no xml
and A problem about injecting spring bean into jsf bean . They talk about injecting Spring managed bean into JSF context.
What I am trying to do must be really simple but am not able to find any relevant info. Am a newbie and will appreciate any help.
I think you may be confused by the word "bean".
The thing is, the "service" you are talking about is also a Spring bean, right?
You probably have it as a service cause it has some additional features (probably transaction management) added by Spring, according to your configuration.
The JSF IoC container is very simplistic, it does not allow you to configure its lifecycle to include transaction management, AOP and things like that. Those things you have to do with Spring (or EJB, in a Java EE environment).
So, when using JSF with Spring, you usually have two choices:
Either you put the backing beans for the JSF pages in the JSF container, annotating them with #ManagedBean, #RequestScoped, #ViewScoped, etc; and injecting any necessary Spring bean with #ManagedProperty in a property (a setter is required)
Or skip the JSF container and put the backing beans along with all others in the Spring container, and use the Spring scopes of request/session, annotating them with Spring's annotations #Component, #Scope("request"), #Scope("session") and injecting with #Autowired, #Qualifier and the like.
Personally, faced with that choice I'd go with the first choice, cause it gives you #ViewScoped and some other niceties. It's true it makes use of two IoC containers but then, which Java EE app does not?
If you want to go the second route anyway, you may also add a view scope for Spring beans, backed by JSF viewMap.
What Spring calls a "Service" is in Java EE terms an "EJB". EJB is out the box available in Java EE web profile containers like Glassfish, JBossAS and TomEE.
To create a stateless EJB service, just use #Stateless on the class:
#Stateless
public class SomeService {
public void doSomething() {
// ...
}
}
And to inject it in a JSF managed bean, just use #EJB on the property-to-be-injected:
#ManagedBean
#ViewScoped
public class SomeController {
#EJB
private SomeService service;
}
That's it. No getter/setter necessary. No XML boilerplate necessary.

jsf 2.0 spring 3 scope request not working

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.
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.
Your approach is a bit confusing in the sense that it seems that you're mixing Spring XML configuration and Spring Annotation-based configuration. As described as an example here, if you're using annotated configuration then you should have:
<context:component-scan base-package="com.yourcom.package" />
to order Spring scan for the annotations. Otherwise, if you're using XML configuration then you should have:
<bean id="helloBean" class="com.mkyong.common.HelloBean" init-method="init" scope="request" />
as by default the scope for a Spring bean is singleton.

How to inject a Spring 3 Bean into a JSF 2 Converter

I am trying to #javax.naming.Inject a Spring 3 Bean called WtvrBean into a JSF 2 #FacesConverter.
Both the Bean and the Converter are on the same package. And, in my spring's applicationContext.xml, I am scanning this package:
<context:component-scan base-package="my-package" />
But this is not working. For sure, the JSF 2 internal class that uses the converter is
definitely not in my-package.
For instance, if I remove the #ManagedBean from a JSF 2 ManagedBean, and replace it to #org.springframework.stereotype.Component or #Controller, the WtvrBean can be #Injected on this ManagedBean, by using Spring WebFlow.
Well, as far as I know, there is no such thing as a #Converter stereotype in Spring.
I know I can use
FacesContextUtils.getWebApplicationContext(context).getBean("WtvrBean")
But, with that approach, the coupling between the web app and the spring is getting more tight. (annotations are metadata, and are not even considered dependency by some authors).
I am using FacesContextUtils so far, if there is no better solution.
Any ideas?
If you want to inject beans into instances of a class, these instances have to be spring-managed. I.e. spring has to instantiate them. And this is not happening, so - no, you can't inject there.
But this is because you register the converter within jsf. You can skip that:
#Component("myConverter")
public class MyConverter implements Converter { .. }
And then, if you are using the spring <el-resolver>:
converter="#{myConverter}"
So that will do it. It has worked for me.
(A workaround worth mentioning is that you can do it by using aspectj weaving and #Configurable, but I'd prefer your FacesContextUtils approach. The weaving modifies classes so that they become spring managed even if they are not instantiated by spring.)
#FacesConverter(value = "examTypeConverter")
#Named
Simple answer.
hey i was facing the same problem that spring beans are not getting injected in the JSF Converter.
then by googling about it i found the answers that after JSF 2.2 we can make converters as jsf managed bean. and then we can inject the spring dependency.
it solved my problem.
#ManagedBean
#RequestScoped
public class CustomerByName implements Converter {
#ManagedProperty(value = "#{customerDao}")
private CustomerDao customerDao;
and in your jsf page use it like a managed bean
converter="#{customerByName}"
Add #Scope annotation (eg with "request" parameter) to your managed bean.
#ManagedBean
#Scope("request")
public class MyBean {
....
}

How to #autowire into jsf managed beans

In order to use the #Autowire annotation, the object where you use the annotation must come from the spring context.
JSF managed beans are created by JSF's IOC not Springs, therefor i cannot use #Autowire inside of them must must use faces-config.xml and managed properties.
I already setup an EL resolver that lets be have spring beans as managed properties, i want to take it one step further and get rid of the need to go into the faces-config.xml every time i need to autowire something. Is this possible?
Just annotate your managed beans with #Controller (or #Component), and #Scope("request") (or session) and add <context:component-scan> (if you haven't), and managed beans will automatically be detected as spring beans. And since you are already using the ELResolver, that should be it - you should be able to use #Autowired (or better - #Inject, if using spring 3.0).
You can use #ManagedProperty(#{'someBean'}) for autowire other beans in jsf bean

Resources