You can find the full source code for this website in the Seam package in the directory /examples/wiki. It is licensed under the LGPL.
| Online: | 17 Members of 4546 |
| Forum: Seam Users |
19. Feb 2008, 12:09 CET | Link |
I want to inject some components for data access etc into my validators. Is it possible to use a hibernate validator class as a seam component? If not, is it possible to reach an instance of a seam component from the validator?
So far I've done this in components.xml
<component name="myDao" class="MyDao" auto-create="true" scope="stateless" /> <component name="myValidator" class="MyValidator" auto-create="true" scope="stateless"> <property name="myDao">#{myDao}</property> </component>myValidator is an instance of org.hibernate.Validator.Validator. myDao is a static property of myValidator, and I attempt to inject it via EL in components.xml.
This doesn't work. When I reach the isValid-method of myValidator, myDao has value null.
Does anyone have a better example of using seam-components from hibernate validator instances?
By the way, I cannot use seam annotations in myDao and myValidator since they are shared by a non-seam project.
You could call Component.getInstance() from inside the validator class, I suppose...
Learn more about Web Beans...
I tried this (in the validators initialize()-method):
this.myDao = (MyDao) Component.getInstance(MyDao.class);
and got
No @Name annotation for class: MyDao
As listed previously, I already configured MyDao in components.xml - so it has no @Name annotation. Is this a bug? Is there another way to get hold of seam components from non-seam components?
How does Contexts.lookupInStatefulContextorsomethinglikethat react?
If a man speaks in the forest and there is no woman around to hear him, is he still wrong?
Use Component.getInstance("myDao");
Read about how to report a bug.
It works IF I already have myDao in a stateful scope. However, this is not so easy to accomplish in components.xml (I cannot annotate as MyDao is shared between projects).
I've created a temporary component that starts up myDao:
@Scope(APPLICATION) @Name("daoHelper") @Startup(depends = "myDao") public class ValidatorHelper {}I really hope to get rid of this one and do something similar to the @Startup-annotation in my components.xml instead. Ideas?
Thanks Pete, that worked without daoHelper to initialize myDao.
Is there an easier way to do this if annotations are an option? Also, what are the repercussions of using Component.getInstance()? That is the method I am currently trying, but it is somehow causing an infinite loop leading to a StackOverflowException when I call flush(). Any help is appreciated.