Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Spring Dependency Injection in JSF Validator

 
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
 
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • 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 Mac OS X IntelliJ IDE Spring Linux
  • 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 Mac OS X IntelliJ IDE Spring Linux
  • 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 Mac OS X IntelliJ IDE Spring Linux
  • 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







 
Your mother was a hamster and your father was a tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic