How can I programmatically change the ParameterNameProvider used by my Spring + Hibernate JPA application?
I tried to create a validation.xml but I keep getting exceptions saying that "parameter-name-provider" is an invalid element.
So I figured I'd prefer to do it programmatically anyway, but I can't figure out how to modify the validator the system will use with my ParameterNameProvider.
I sounds to me that you are not using the right version of Bean Validation. If parameter-name-provider is an invalid element, you might be using the Bean Validation 1.0 API, instead of 1.1. Programmatically it looks for example like this:
ValidatorFactory factory = Validation
.byDefaultProvider().configure()
.parameterNameProvider( new MyParameterNameProvider() )
.buildValidatorFactory();
But again, you need Bean Validation 1.1.
Related
I am having a JSF application and I am using Spring for bean management. I registered all JSF beans as Spring beans and everything was running very smoothly. Now, I have got a requirement to use JSF's view scope, so it means I have to create a JSF managed bean (which I did using JSF annotations and all).
Now, I want to access this JSF bean in a Spring bean, certainly I cannot inject or get it using Spring's application context because Spring container doesn't know about this bean. So, currently in my Spring bean code I am getting this JSF bean using JSF's EL Resolver (examples mentioned here).
What I want to know is that is there any better way to do this?
My application is on Spring 4.0 and JSF 2.0 (MyFaces implementation), please let me know if you need any other information. I have not placed code because all my code is in remote server, moreover I think code is not required for this as there is no debugging over here.
I have some problems with FlushMode.AUTO in JPA/Hibernate, because i need change a value of a JavaBean and process a special validation. If validation is ok the bean is updated in DB, if validation fail the bean cannot be updated, like this:
MyBean bean = getBeanFromDB();
bean.setNewNumber(12);
//Before call isValid the bean already updated in DB
if (isValid(bean)){
update(bean);
}
But before validation begins, the Hibernate process the AUTO-UPDATE in my Bean and i don't wanna it. So my solution is setting FlushMode.COMMIT in JPA, but i'm using Spring and i don't know how can i do it in CONFIGURATION (XML) mode.
I see two options:
use bean validation (aka JSR-303) and its JPA integration to ensure automatic validity
use #PrePersist and #PreUpdate events to have your validation logic invoked automatically
I made a JAX-RS service. It consumes/produces json via POST requests. Everything works fine except bean validation
I added some constraints from package javax.validation.constraints to the fields of my POJOs, that are used as wrappers for request/response messages.
(POJOs does not have any other annotations like JAXB #XmlRootElement or smth else)
If I try to call my service - it works, but constraints ignored completely. I.e. I call my service using right or wrong parameters and calls passed RAX-RS layer to my EJB in both cases.
What could I miss? Some special config, some special dependencies?
P.S. I use glassfish 4.0 with provided Jersey 2.0
In order for bean validation to be invoked you need to have a validator on your classpath. A commonly used validator is the hibernate-validator as it is the reference implementation.
http://www.hibernate.org/subprojects/validator.html
Once you have the validator on the classpath it will be detected and invoked when JSR-303 annotations are encountered.
I want to clear some moments about integrating spring and struts. I have only one action class per application extended from MappingDispatchAction. So, actually my app when doing something uses not Action objects, but methods from my action. All I want from spring is to initialize this action and all for now. Just simply set DAO object. I looked through documentation, but I don't understand following:
We use action path from struts-config.xml as a name of bean in action-servlet.xml. Okay, but am I supposed to write beans in action-servlet.xml for every path name and set this poor DAO ref or what ?
The Struts 1 config file will use the DelegatingActionProxy class as the type attribute for all action configurations.
The Spring config file will contain the bean definitions of each action implementation. I don't know what DAO you're talking about, but actions that require DAO or service injection need to have them listed, yes--that's what Spring configuration is.
You may also be able to use annotations if you're not interested in using XML configuration, or use bean inheritance if many beans share the same DAO/service/etc. property values.
I am wondering how others might have accomplished this. I found in the Spring documentation #required which I attempted to use in a 'test' but got this stmt INFO XmlConfigurationProvider:380 - Unable to verify action class [xxx] exists at initialization
I have found another way in spring to do this is to write custom init methods and declare those on the bean but it almost seems like this is a strange thing to do to a struts2 action.
In my application I inject through spring different web services (via setter injection) into different actions. I want to ensure on startup these are not null and set.
Use request or session spring scopes.
Then you can do the checks in a #PostConstruct method, what will you do there? Throw an exception? Well, something isn't set you will eventually get a NullPointerException.