Jaxb2Marshaller xsd schema validation query - spring

I am using spring 3.0.6 Jaxb2Marshaller using below configuration,
<bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="contextPaths">
<list>
<value>com.xxx.yyy.schema.external_request.event</value>
<value>com.xxx.yyy.schema.zzz.external_request</value>
</list>
</property>
</bean>
I want to validate xml against against the external_request.xsd which it is not doing. Do I need to explicitly pass the schema property even if the generated sources have the annotation which mentions that element is required something like #XmlElement(name = "abc", required = true). Has any one already faced this issue. Thanks!

Hey may be instead of adding classes to context paths you can try adding it to classesToBeBound proprty
<bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>com.xxx.yyy.schema.external_request.event</value>
<value>com.xxx.yyy.schema.zzz.external_request</value>
</list>
</property>
</bean>
Please rate the answer if it helps.
cheers

Related

How to use AfterAdvice Interface in java

I am new to spring.I know that AfterAdvice will cause the after method to execute whether target method completes or exits with exception, but i am not able to find out any example for it.
As AfterAdvice is a marker interface, I don't know which method i need to define in it's implementation class.
Thanks,
You do not have to implement those interfaces directly. Instead, you use either
Use #After annotation to mark the method you want to it to be called.
Use spring xml bean configuration aop:advice to declare an after advice method
However, if you choose to use ProxyFactoryBean
As you indicate that you want to use ProxyFactoryBean, you can declare the xml like this
<bean id="interceptor"
class="yourimplementation">
</bean>
<bean id="setterAdvisor"
class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice">
<ref bean="interceptor"/>
</property>
<property name="patterns">
<list>
<value>.*set.*</value>
</list>
</property>
</bean>
<bean id="person"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces" value="com.mycompany.Person"/>
<property name="target" ref="personTarget"/>
<property name="interceptorNames">
<list>
<value>setterAdvisor</value>
</list>
</property>
</bean>
For the java implementation, there is no use to implement Advice interface. You should either implement ThrowingAdvice or AfterReturningAdvice. Refer to this for more info.
For more information, you can refer to a couple tutorial guides on Spring AOP and play around with it to get a sense.

spring-batch : load and use a property file

I am new on Spring batch so am here to ask some basic advice.
What is the best approach to load a config file in memory (or bean) and use its content while the spring Job/step are running ?
I am not sure but based on some google search I found the below scenario even if I dont quite understand why I should define a writer even if i dont need it :
step1 : load config file (the content is two field delimited by =)
step2 : perform some java code and use the previous config file
so for the step 1 :
<bean id="inputFile" class="org.springframework.core.io.FileSystemResource" scope="step">
<constructor-arg value="path_config_file"/>
</bean>
<bean id="readerConfigFile" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step">
<property name="resource" ref="inputFile"/>
<property name="lineMapper">
<bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
<property name="lineTokenizer">
<bean class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
<property name="names" value="field,value"/>
<property name="delimiter" value="="/>
</bean>
</property>
<property name="fieldSetMapper">
<bean class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
<property name="prototypeBeanName" value="configProperties"/>
</bean>
</property>
</bean>
</property>
</bean>
<bean id="outputConfig" class="outputConfig"></bean>
<bean id="configProperties" class="configProperties" scope="prototype"/>
so my question are :
How can I use the information gathered in the file ? Should I put them in the Java bean ?
How can I pas this info between different step or make them persistent in the whole application life-cycle ?
Would you recommend to use a itemProcessor to achieve the above ?
Any advice are most than welcome
I'm a bit confused about your questions because I think you only need to load a properties file in spring context using a PropertiesFactoryBean:
<bean id="config" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location">
<value>file:path_config_file</value>
</property>
</bean>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="propertiesArray">
<list>
<ref bean="config"/>
</list>
</property>
</bean>
and you can refer to property values using ${} placeholder; but this solution is unrelated to spring-batch; I don't need it!
About your questions:
Using a POJO is a good way because spring-batch offers in-box mapping strategies (BeanWrapperFieldSetMapper in your case)
Objects used in a job are accessible only in job context, not in application context (this is why I think you need a PropertiesFactoryBean).To pass object between steps read How can we share data between the different steps of a Job in Spring Batch?
ItemProcessor is requested if you need to convert an object T read from a ItemReader<T> to an object of type S written by an ItemWriter<S>. So no, you don't need an ItemProcessor.
I hope I was clear, English is not my native language

Loading properties from a file AND system properties using Spring

Searched through some other posts but could not find exactly what I needed, but I would guess this is an easy question..
So I have a property file called myprops.properties
myprops.localProp1=localProp1
myprops.localProp2=localProp2
myprops.systemProp=${systemPropertyName}
Basically, in this property file I want to use the values as is for localProp1 and locapProp2 but for systemProp, I would like to load the system property. Let's assume that the system property is always set.
My spring config xml looks like this...
<bean id="myprops" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<qualifer value="myprops" />
<property name="singleton" value="true"/>
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list><value>classpath:myprops.properties</value></list>
</property>
</bean>
I use the qualifier have this bean autowired and use the qualifier string "myprops" to access it in another class. All the expected values are there except the myprops.systemProp, it still = ${systemPropertyName}.
How would I get this property to be resolved with the actual system property?
I tried the following in my spring config:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="properties" ref="myprops" />
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
</bean>
This didn't seem to help..
Any ideas? I'm hoping this is an easy one and I am just misunderstanding a common concept in property configuration.
Note: I had to manually type all the code couldn't copy/paste so please excuse typos.
Thanks.

Migrating Hibernate .hbm to annotations

I am currently migrating an older application from hbm mappings to annotations. I have read numerous places stating that it is possible to do this, including the hibernate docs. I am running into trouble when I try to reference an annotated class from an hbm mapped with a many-to-one relationship.
I am getting the following error complaining about the annotated class.
"nested exception is org.hibernate.MappingException: Association references unmapped class: ...cepis.domain.Note"
Is it possible to achieve what I want here, is there something very basic that I am missing (note like I said I have read numerous times that it is possible so a simple "yes it is possible" will not be of much help)?
My session factory is defined as follows ...
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
scope="singleton">
<property name="dataSource" ref="itc5DataSource" />
<property name="mappingResources">
<list>
<value>edu/uky/cepis/User.hbm.xml</value>...
</list>
</property>
<property name="annotatedClasses">
<list>
<value>edu.uky.cepis.domain.Note</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${itc5.dialect}</prop>
...
</props>
</property>
</bean>
Thank you so much for any help in advance!
have you tried setting _ hibernate.mapping.precedence_ to "class, hbm"?

Spring MVC from 2.5 to 3.0

We are migrating our web application from Spring 2.5 to Spring 3.0.5. Looks like all the Controller classes (the classes in org.springframework.web.servlet.mvc package: BaseCommandController, AbstractCommandController, SimpleFormController, etc) have been deprecated. We used those classes heavily in our application. These classes have been replaced by #Controller annotation instead.
I have a few questions regarding Spring 3.0.x Controller configuration...
We used the following XML to create a Controller bean in Spring 2.5. If <context:component-scan> is used instead to create the Controller beans, then how do I wire-in the dependencies? If possible I would like to avoid Auto-Wiring.
<bean id="photosetViewController" class="com.xyz.web.PhotosetViewController"
p:photosetManager-ref="photosetManager"
p:photoManager-ref="photoManager" />
We have created 3 Interceptors. We use SimpleUrlHandlerMapping to map these Interceptors to different request URLs and also to map URLs to Controllers. How do we achieve the same in Spring 3.0.x?
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="alwaysUseFullPath" value="true" />
<property name="interceptors">
<list>
<ref bean="httpRedirectInterceptor"/>
<ref bean="loginInterceptor"/>
</list>
</property>
<property name="mappings">
<value>
/abc.html=abcLookupController
/photoupload.html=photoUploadController
</value>
</property>
</bean>
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="alwaysUseFullPath" value="true" />
<property name="interceptors">
<list>
<ref bean="httpRedirectInterceptor"/>
<ref bean="loginInterceptor"/>
<ref bean="userMenuInterceptor" />
</list>
</property>
<property name="mappings">
<value>
/index.html=homepageController
/success.html=simpleController
</value>
</property>
</bean>
In case of SimpleFormControllers we used different methods like initBinder, referenceData, formBackingObject. We also used command objects and validation classes. How do we achieve the same in Spring 3.0.x?
<bean id="photosetAddController" class="com.xyz.web.PhotosetAddController"
p:photosetManager-ref="photosetManager"
p:photosetTypeManager-ref="photosetTypeManager"
p:stateManager-ref="stateManager" >
<property name="validator">
<bean class="com.xyz.form.PhotosetAddValidator" />
</property>
<property name="formView" value="photosetadd" />
<property name="successView" value="redirect:/photoset" />
</bean>
Any pointers are greatly appreciated.
As skaffman noted - your code will work fine without any modifications. I'll answer your questions in short:
You can use #Resource(name="beanName") for name-based injection. But autowiring is also a good option. Either #Autowired, or #javax.inject.Inject. Note that you can use #Qualifier("name") to differentiate between beans with the same interface/base class. You can even use the javax.inject qualifiers (read about all these in the docs)
interceptor mappings can stay the same
There is #InitBinder, which denotes the initBinder method. I can't tell about the other.

Resources