How to insert Spring 3 Bean into JSF 2 Converter

I am trying @javax.naming.Inject a Spring 3 Bean called WtvrBean in JSF 2 @FacesConverter .

Both Bean and Converter are in the same package. And, in my Spring applicationContext.xml, I am looking at this package:

 <context:component-scan base-package="my-package" /> 

But that does not work. Of course, the inner JSF 2 class that uses the converter is definitely not in my-package .

For example, if I @ManagedBean from JSF 2 ManagedBean and replace it with @org.springframework.stereotype.Component or @Controller , WtvrBean could be @Inject ed on that ManagedBean using Spring WebFlow.

Well, as far as I know, Spring doesn't have the @Converter stereotype.

I know I can use

 FacesContextUtils.getWebApplicationContext(context).getBean("WtvrBean") 

But with this approach, the connection between the web application and Spring becomes more stringent. (annotations are metadata and are not even considered by some authors dependent).

I am using FacesContextUtils so far if there is no better solution.

Any ideas?

+7
source share
4 answers

If you want to insert beans into class instances, these instances must be spring-driven. That is, spring must create them. And this does not happen, so - no, you cannot enter there.

But this is due to the fact that you register the converter in jsf. You can skip this:

 @Component("myConverter") public class MyConverter implements Converter { .. } 

And then if you use spring <el-resolver> :

 converter="#{myConverter}" 

So it will be done. It worked for me.

(A workaround is to mention that you can do this using aspectj weaving and @Configurable , but I would prefer your FacesContextUtils . Weaving modifies classes so that they become spring-driven, even if they were not instantiated by spring.)

+12
source
 @FacesConverter(value = "examTypeConverter") @Named 

Simple answer.

+2
source

Hey, I ran into the same problem that spring beans are not injected into the JSF converter.

and then by the search query I found the answers that after JSF 2.2 we can make the converters as a jsf managed bean. and then we can introduce the spring dependency. he solved my problem.

  @ManagedBean @RequestScoped public class CustomerByName implements Converter { @ManagedProperty(value = "#{customerDao}") private CustomerDao customerDao; 

and on your jsf page use it as a managed bean

  converter="#{customerByName}" 
0
source

Add the @Scope annotation (for example, with the query parameter) to your managed bean.

 @ManagedBean @Scope("request") public class MyBean { .... } 
-one
source

All Articles