File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Spring and the fly likes Spring Dependency Injection in JSF Validator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Spring Dependency Injection in JSF Validator" Watch "Spring Dependency Injection in JSF Validator" New topic
Author

Spring Dependency Injection in JSF Validator

Niall Loughnane
Ranch Hand

Joined: Dec 07, 2006
Posts: 208
Hi,

I have a JSF validator and want to inject a dependency injection into it of a component,

Is this possible in a JSF Validator?

The JSF Validator code is:

[code=java]
@FacesValidator("jsfValidator")
public class JSFValidator implements Validator {


public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
.....
}
}
[/code]

And I want to inject a service "ServiceImpl" into the above class,

The validator is called from a XHTML file using:
[code=java]
<h:inputText id="display" value="#{bean.value}" >
<f:validator validatorId="jsfValidator"/>
<f:attribute name="displayName" value="#{displayName}" />
</h:inputText>
[/code]

Thanks in Advance,

Niall
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1666
    
    7

Spring will only do dependency injection in Spring managed beans.

[How To Ask Questions][Read before you PM me]
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1666
    
    7

You can work around this by using the spring-configured tag with the @Configurable annotation.

See the following from the reference manual.
http://static.springsource.org/spring/docs/3.1.1.RELEASE/spring-framework-reference/html/aop.html#aop-atconfigurable

I have used this for a few things before and it does work.
Niall Loughnane
Ranch Hand

Joined: Dec 07, 2006
Posts: 208
@Configurable is used with AOP, can this also be used with JSF Validators?
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1666
    
    7

Huh? AOP is aspect oriented programming it is not specific to a specific type of object.

There are different configuration options as defined in the documentation I linked but in short:

1. Put @Configurable on your JSFValidator class.
2. @Autowire in your service in your JSFValidator (Make sure the Service you are autowiring in is registered spring bean)
3. Add <context:spring-configured/> to the application context
4. Make sure spring-aop is in included in your maven dependencies.

I am not familiar with how the validator is created by JSF so no guarantees but it might work.
Niall Loughnane
Ranch Hand

Joined: Dec 07, 2006
Posts: 208
Cool, Thanks for the info, Will give it a go :)

Thanks,

Niall
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1666
    
    7

I probably should not assume you have the aspectj plugin configured in your maven. I don't have any of my projects in front of me to reference but something like below should work







 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Spring Dependency Injection in JSF Validator