Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Spring Dependency Injection in JSF Validator

 
Niall Loughnane
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Spring will only do dependency injection in Spring managed beans.
 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Configurable is used with AOP, can this also be used with JSF Validators?
 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cool, Thanks for the info, Will give it a go :)

Thanks,

Niall
 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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







 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic