How do I set up custom Mongo formatters in Spring? - spring

I've been at this for a few hours and haven't found anyone that's gotten this working yet. I want to persist a BigDecimal object in Mongo, but Mongo doesn't natively support BigDecimal. I followed Spring's docs here but no luck.
From what I can tell Spring isn't injecting my custom converter classes into Mongo when it's writing to the db. Here's what I have done:
My applicationContext-services.xml
...
<!-- Factory bean that creates the Mongo instance -->
<mongo:mongo
host="localhost"
port="1234" />
<mongo:db-factory
dbname="solar"
mongo-ref="mongo"/>
<mongo:mapping-converter>
<mongo:custom-converters>
<mongo:converter>
<bean class="com.mine.BigDecimalReadConverter"/>
</mongo:converter>
<mongo:converter>
<bean class="com.mine..BigDecimalWriteConverter"/>
</mongo:converter>
</mongo:custom-converters>
</mongo:mapping-converter>
<!-- Use this post processor to translate any MongoExceptions thrown in #Repository annotated classes -->
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
<bean id="mongoDbTest"
class="com.mine.MongoDbTest">
<property name="mongoTemplate">
<ref local="mongoTemplate" />
</property>
</bean>
The error I'm getting is:
java.lang.IllegalArgumentException: Multiple constructors with arguments found in class java.math.BigDecimal! Annotate one with #PreferedConstructor explicitly to select it to be used in persistence operations.
at org.springframework.data.mapping.PreferredConstructorDiscoverer.<init>(PreferredConstructorDiscoverer.java:81)

Try using this for converter support:
<bean id="mappingContext"
class="org.springframework.data.mongodb.core.mapping.MongoMappingContext" lazy-init="true"/>
<bean id="defaultMongoTypeMapper"
class="org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper" lazy-init="true">
<constructor-arg name="typeKey"><null/></constructor-arg>
</bean>
<bean id="mappingMongoConverter"
class="org.springframework.data.mongodb.core.convert.MappingMongoConverter" lazy-init="true" >
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
<constructor-arg name="mappingContext" ref="mappingContext" />
<property name="typeMapper" ref="defaultMongoTypeMapper" />
</bean>
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate" lazy-init="true">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
<constructor-arg name="mongoConverter" ref="mappingMongoConverter" />
</bean>

Here is how I solved this. The order in which you define the beans matters. So my app.xml that I got it working with is:
<bean id="mappingContext" class="org.springframework.data.document.mongodb.mapping.MongoMappingContext"/>
<bean id="readConverter" class="com.mine.BigDecimalReadConverter"/>
<bean id="writeConverter" class="com.mine.BigDecimalWriteConverter"/>
<mongo:mapping-converter id="mappingConverter">
<mongo:custom-converters>
<mongo:converter ref="readConverter" />
<mongo:converter ref="writeConverter" />
</mongo:custom-converters>
</mongo:mapping-converter>
<!-- Factory bean that creates the Mongo instance -->
<mongo:mongo
host="${${environment}.mongodb.host}"
port="${${environment}.mongodb.port}" />
<mongo:db-factory
dbname="${${environment}.mongodb.databaseName}"
mongo-ref="mongo"/>
<bean id="mongoTemplate" class="org.springframework.data.document.mongodb.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
<constructor-arg name="mongoConverter" ref="mappingConverter"/>
</bean>
<!-- Use this post processor to translate any MongoExceptions thrown in #Repository annotated classes -->
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>

Related

Spring: RestTemplate messageConverters are not registered

I have an issue with the registration of the messageConverters on the org.springframework.web.client.RestTemplate.
I have tried (with no effect) two solutions:
1) Defining a bean of restTemplate in the Spring configuration, with all the converters that I need:
<bean id="restTemplate" name="restTemplate" class="org.springframework.web.client.RestTemplate" autowire-candidate="true">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.FormHttpMessageConverter"/>
<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
<bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
<bean class="org.springframework.http.converter.ResourceHttpMessageConverter"/>
<bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"/>
<bean class="org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter"/>
<bean class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter"/>
<bean class="test.myApp.MyHttpMessageConverter" />
</list>
</property>
</bean>
2) Declaring the messageConverters in the <mvc:annotation-driven> tag:
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.FormHttpMessageConverter"/>
<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
<bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
<bean class="org.springframework.http.converter.ResourceHttpMessageConverter"/>
<bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"/>
<bean class="org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter"/>
<bean class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter"/>
<bean class="test.myApp.MyHttpMessageConverter" />
</mvc:message-converters>
</mvc:annotation-driven>
I also used the attribute register-defaults="true"
With both of these two solutions, the restTemplate instance contains only the same 6 default converters:
ByteArrayHttpMessageConverter
StringHttpMessageConverter
ResourceHttpMessageConverter
SourceHttpMessageConverter
AllEncompassingFormHttpMessageConverter
Jaxb2RootElementHttpMessageConverter
There is no trace of the test.myApp.MyHttpMessageConverter and of the org.springframework.http.converter.FormHttpMessageConverter.
I'm using the 4.3.1.RELEASE version of Spring.
How to make possible the registration of the messageConverters?
Thanks in advance.
I have found the solution.
Since I'm using the Spring Integration <int-http:outbound-gateway> component, I need to add the attribute "rest-template" in the outbound-gateway.
So, the working solution is the (1) that I posted above, together with this configuration:
<int-http:outbound-gateway rest-template="restTemplate" ... />
Otherwise, it will be used a default rest-template (without custom messageConverters).

Spring factory-bean with property resolution

Is it possible to have property resolution on the factory-bean field of a Spring bean declaration?
Example:
<bean factory-bean="$APP{some.factory.bean}" factory-method="...">
Spring version: 3.2.4
Adding a PropertyPlaceholderConfigurer to your spring context should work:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:yourfile.properties</value>
</list>
</property>
<property name="ignoreResourceNotFound" value="false" />
<property name="ignoreUnresolvablePlaceholders" value="false" />
</bean>
You can declare your propertyName in the file and then use it like:
<bean factory-bean="${propertyName}" factory-method="...">
Just encountered the same issue (using Spring 4.2.0). It seems that property resolution for factory beans does not work despite what they say in SPR-12638. I've ended up using ProxyFactoryBean as a workaround.
The desired configuration, which does not work:
<!-- The bean to be created: factory type determined in runtime -->
<bean id="..." factory-bean="factory-${factory.type}" factory-method="..." />
<!-- Possible factories -->
<bean id="factory-A" class="..." />
<bean id="factory-B" class="..." />
<bean id="factory-C" class="..." />
The workaround that I've found:
<!-- The bean to be created: use factory proxy -->
<bean id="..." factory-bean="factory-proxy" factory-method="..." />
<!-- The factory proxy: real factory type determined in runtime -->
<bean id="factory-proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="factory-${factory-type}" />
</bean>
<!-- Possible factories -->
<bean id="factory-A" class="..." />
<bean id="factory-B" class="..." />
<bean id="factory-C" class="..." />
Enjoy :)

Regarding Spring Batch

I am very new to Spring Batch. I just tried to run a simple spring batch example which i got from net in vain.The example just tries to read from a flat file and write the contents into a separate flat file.
Below is my xml which is loaded into context (through a ContextLoaderListener).
test-batch.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://172.18.7.57/spring-beans.dtd">
<beans>
<bean id="simpleJob"
class="org.springframework.batch.core.job.SimpleJo b">
<property name="name" value="simpleJob" />
<property name="steps">
<list>
<ref local="step" />
</list>
</property>
<property name="jobRepository" ref="jobRepository" />
</bean>
<bean id="jobLauncher"
class="org.springframework.batch.core.launch.suppo rt.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository" />
</bean>
<bean id="jobRepository"
class="org.springframework.batch.core.repository.s upport.SimpleJobRepository">
<constructor-arg>
<bean
class="org.springframework.batch.core.repository.d ao.MapJobInstanceDao" />
</constructor-arg>
<constructor-arg>
<bean
class="org.springframework.batch.core.repository.d ao.MapJobExecutionDao" />
</constructor-arg>
<constructor-arg>
<bean
class="org.springframework.batch.core.repository.d ao.MapStepExecutionDao" />
</constructor-arg>
</bean>
<bean id="transactionManager"
class="org.springframework.batch.support.transacti on.ResourcelessTransactionManager" />
<bean id="step"
class="org.springframework.batch.core.step.item.Si mpleStepFactoryBean">
<property name="transactionManager" ref="transactionManager" />
<property name="jobRepository" ref="jobRepository" />
<property name="itemReader" ref="itemReader" />
<property name="itemWriter" ref="itemWriter" />
</bean>
<bean id="lineAggregator"
class="org.springframework.batch.item.file.transfo rm.DelimitedLineAggregator">
<property name="delimiter" value="|" />
</bean>
<bean id="itemWriter"
class="org.springframework.batch.item.file.FlatFil eItemWriter">
<property name="fieldSetCreator" ref="fieldSetMapper" />
<property name="lineAggregator" ref="lineAggregator" />
<property name="resource" value="file:/hello2.txt" />
</bean>
<bean id="lineTokenizer"
class="org.springframework.batch.item.file.transfo rm.DelimitedLineTokenizer" />
<bean id="fieldSetMapper"
class="org.springframework.batch.item.file.mapping .PassThroughFieldSetMapper" />
<bean id="itemReader"
class="org.springframework.batch.item.file.FlatFil eItemReader">
<property name="resource" value="file:/hello1.txt" />
<property name="lineTokenizer" ref="lineTokenizer" />
<property name="fieldSetMapper" ref="fieldSetMapper" />
</bean>
</beans>
As i dont have maven, i tried launching this job from my java code like ,
ApplicationContext context = new FileSystemXmlApplicationContext("D:\\jboss-4.0.5.GA\\server\\default\\deploy\\test.war\\WEB-INF\\xml\\test-batch.xml");
Job job;
job = (Job) context.getBean("simpleJob");
JobParameters jobParameters = jobParametersConverter.getJobParameters(StringUtils
.splitArrayElementsIntoProperties(new String[]{"D:\\jboss-4.0.5.GA\\server\\default\\deploy\\venki.war\\WEB-INF\\xml\\venki-batch.xml","simpleJob"}, "="));
JobExecution jobExecution = launcher.run(job, jobParameters);
--But, some error is thrown in eclipse saying no class loaders found....below is the error i got....
org.apache.commons.lang.SerializationException: java.lang.ClassNotFoundException: No ClassLoaders found for: org.springframework.batch.core.JobExecution
at org.apache.commons.lang.SerializationUtils.deseria lize(SerializationUtils.java:164)
at org.apache.commons.lang.SerializationUtils.deseria lize(SerializationUtils.java:191)
at org.springframework.batch.core.repository.dao.MapJ obExecutionDao.copy(MapJobExecutionDao.java:33)
at org.springframework.batch.core.repository.dao.MapJ obExecutionDao.saveJobExecution(MapJobExecutionDao .java:56)
at org.springframework.batch.core.repository.support. SimpleJobRepository.saveOrUpdate(SimpleJobReposito ry.java:216)
at org.springframework.batch.core.repository.support. SimpleJobRepository.createJobExecution(SimpleJobRe pository.java:192)
at org.springframework.batch.core.launch.support.Simp leJobLauncher.run(SimpleJobLauncher.java:79)
at com.venki.handler.TestHandler.testSpringBatch(Test Handler.java:111)
--Please help me in this regard...i need to find out a code that reads from a flat file and inserts into a DB....i need to integrate this into a stand alone module and i should not go with using maven....
Why dont you run it by using org.springframework.batch.core.launch.support.CommandLineJobRunner
you can define it as your main class in Run Configuration of eclise.
In Arguments give
<your batch-job-xml-file><space><your Job (in your case its simpleJob)>
You dont need maven here too. Just set classpath of required libraries.
The sample Spring Batch football project reads from a CSV file and write into a database using JDBC.
You can find the details here, and the download links are at the top of that page.
As far as Maven goes, you don't need it, but you'll have to manually include all the required libraries on your classpath. If you're running it through Eclipse, trigger the CommandLineJobRunner.

how to use jotm with jpa and neo4j

I configured my application based on spring,jpa and neo4j to use JOTM for transaction management with the following configuration.
<context:annotation-config />
<bean id="jotm" class="org.springframework.data.neo4j.transaction.JotmFactoryBean"/>
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
<bean id="graphDatabaseService" class="org.neo4j.kernel.EmbeddedGraphDatabase"
destroy-method="shutdown" scope="singleton">
<constructor-arg index="0" value="${neo4j.dblocation}" />
<constructor-arg index="1">
<map>
<entry key="TXMANAGER_IMPLEMENTATION" value="spring-jta" />
</map>
</constructor-arg>
</bean>
<neo4j:config graphDatabaseService="graphDatabaseService" />
<tx:annotation-driven mode="aspectj" transaction-manager="transactionManager" />
But I am facing with two instances of the class org.springframework.transaction.jta.JtaTransactionManager: jtaTransactionManager, neo4jTransactionManager.
And my log :
Using JTA UserTransaction: org.objectweb.jotm.Current#81e88
Using JTA TransactionManager: org.objectweb.jotm.Current#81e88
Using JTA UserTransaction: org.neo4j.kernel.impl.transaction.UserTransactionImpl#6cc760
Using JTA TransactionManager: org.neo4j.kernel.impl.transaction.SpringTransactionManager#195ab57
Is my configuration correct and which one should use?
Thank you

Spring resource

Problem: I want to make the below bean definitions (specified in aaplicationContext.xml) optional for "org.springframework.web.context.ContextLoaderListener". If i am not providing the "emsPropLocation" context parameter correctly, tomcat web container is not able to initialized properly and it is obvious reason. Is there any way to make it optional?
appicationContext.xml:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreResourceNotFound" value="false"/>
<property name="location" value="file:/#{contextParameters.emsPropLocation}" />
</bean>
<!-- TIBCO Connection Factory Bean -->
<bean id="tibcoConnectionFactory" class="com.tibco.tibjms.TibjmsConnectionFactory">
<constructor-arg value="${emsServerURL}"/>
<property name="userName" value="${emsUserName}"/>
<property name="userPassword" value="${emsPassword}"/>
<property name="connAttemptCount" value="${connAttemptCount}"/>
<property name="connAttemptDelay" value="${connAttemptDelay}"/>
<property name="connAttemptTimeout" value="${connAttemptTimeout}"/>
<property name="reconnAttemptCount" value="${reconnAttemptCount}"/>
<property name="reconnAttemptDelay" value="${reconnAttemptDelay}"/>
<property name="reconnAttemptTimeout" value="${reconnAttemptTimeout}"/>
</bean>
<!-- bean id="tibcoUtil" class="com.nr.ns.upload.TibcoUtil" scope="singleton">
<constructor-arg value="true"/>
</bean-->
<bean id="jmsExceptionListener" class="com.nr.ns.upload.LogMsgExceptionListener"/>
<!-- Spring CachingConnectionFactory Bean -->
<bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
<constructor-arg ref="tibcoConnectionFactory"/>
<property name="reconnectOnException" value="${reconnectOnException}"/>
<property name="sessionCacheSize" value="${sessionCacheSize}"/>
<property name="exceptionListener" ref="jmsExceptionListener"/>
</bean>
<!-- JMSTemplate Bean -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<constructor-arg ref="connectionFactory"/>
<property name="receiveTimeout" value="${receiveTimeout}"/>
<property name="deliveryMode" value="${deliveryMode}"/>
</bean>
We are keeping WAR file outside tomcat and to make it happen we have "app.xml" file inside TOMCAT_HOME/conf/Catalina/localhost.
app.xml:
<Context path="/app"
docBase="/abc/ccp/app.war"
reloadable="true"
unpackWAR="false">
<Parameter name="emsPropLocation"
value="/xyz/config/EMSServerConf.properties"
override="false"/>
</Context>
have a try to change the ignoreResourceNotFound property of your propertyConfigurer to true.
If contextParameters.emsPropLocation is not set, this will default to what is afer the colon.
<property name="location" value="file:/#{contextParameters.emsPropLocation:/xyz/config/EMSServerConf.properties}" />

Resources