IoC spring.net injecting System.Type - inversion-of-control

I am trying to initiate a class that is expecting a System.Type in it's CTOR.
Is there a way in spring.net config file to accomplish this, and preferable pass the type of a spring initialised object?
Thanks,
Lihnid

This also works:
<constructor-arg name="argumentname" value="MyNamespace.MyType, MyAssembly"/>
Use VS2010 Add-In for intellisense:
http://www.springframework.net/vsaddin/

I think that this should do it:
<constructor-arg name="argumentname" expression="T(namespace.to.my.type, assemblyname)" />

You can use an expression to inject a type:
<!-- use an expression to specify type -->
<constructor-arg type="System.Type, mscorlib"
expression="T(MyNameSpace.MySecondClass)" />
To get the type of a spring-managed object, you can use the expression="#(object-id-here)" syntax to use a spring-managed object in your expression and simply call GetType() on it:
<!-- inject the type of MySecondObject, configured elsewhere -->
<constructor-arg type="System.Type, mscorlib"
expression="#(MySecondObject).GetType()" />

Just in case if anyone is wondering how to do this with using the System.Type as the dictionary key.
<object id="myDictionary" type="System.Collections.Generic.Dictionary<System.Type,string>">
<constructor-arg name="dictionary">
<dictionary key-type="System.Type" value-type="string">
<entry value="myStringValue">
<key>
<expression value="T(namespace.to.MyClassType)"></expression>
</key>
</entry>
</dictionary>
</constructor-arg>
</object>

Related

No property called 'resource' in MultiResourceItemReader

In spring Batch doc (http://docs.spring.io/spring-batch/reference/html/scalability.html), under 7.4.3 section it is given that we can set the 'resource' property of MultiResourceItemReader from stepExecutionContext. But there is no property called 'resource' in MultiResourceItemReader, instead it is 'resources'.
Then how single resource can be set to MultiResourceItemReader from stepExecutionContext which will have single fileName in each context which was set during partitioning.
Instead it's called resources (which is an array) and it can be set like so:
<bean id="multiResourceReader"
class=" org.springframework.batch.item.file.MultiResourceItemReader">
<property name="resources" value="file:some/folder/prefix*.csv" />
<property name="delegate" ref="flatFileItemReader" />
</bean>
When partitioning, you would not use a MultiResourceItemReader. Instead, just use a FlatFileItemReader in step scope.
<bean id="flatFileItemReader" scope="step"
class="org.springframework.batch.item.file.FlatFileItemReader">
<property name="resource" value="file:#{stepExecutionContext['FILE.NAME']}">
</bean>

How to disable automatic Bean Validation in JPA entities

I'm using Bean Validation to check constraints on my model, but I don't know how to configure it so it only validates when I want it to. I found on that I could put this tag in my persistence.xml, <validation-mode>NONE</validation-mode> but it doesn't work.
I appreciate any kind of help.
I remember that i also had problems with that, here is my working example:
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="punit" />
<property name="jpaPropertyMap">
<map>
<entry key="javax.persistence.validation.mode" value="none"/>
</map>
</property>
</bean>

Error with Spring ldap pooling

i build async jersey web services, and now i need to make some operations with ldap.
I have configure Spring beam.xml in this mode:
<bean id="contextSourceTarget" class="org.springframework.ldap.core.support.LdapContextSource">
<property name="url" value="${ldap.url}" />
<property name="base" value="${ldap.base}" />
<property name="userDn" value="${ldap.userDn}" />
<property name="password" value="${ldap.password}" />
<property name="pooled" value="false" />
</bean>
<bean id="contextSource"
class="org.springframework.ldap.pool.factory.PoolingContextSource">
<property name="contextSource" ref="contextSourceTarget" />
</bean>
<bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate">
<constructor-arg ref="contextSource" />
</bean>
<bean id="ldapTreeBuilder" class="com.me.ldap.LdapTreeBuilder">
<constructor-arg ref="ldapTemplate" />
</bean>
<bean id="personDao" class="com.me.ldap.PersonDaoImpl">
<property name="ldapTemplate" ref="ldapTemplate" />
</bean>
But when i try to use ldap i have this error:
Error creating bean with name 'contextSource' defined in class path resource [config/Beans.xml]: Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: org/apache/commons/pool/KeyedPoolableObjectFactory
In my project i have commons-pool2-2.2.jar lib, but still i have this error..i try to add commons-pool2-2.2.jar in TOMCAT_PATH/lib but not works..
UPDATE:
If i put commons-pool-1.6.jar it works.. but if i want to use pool2 how i can do? only i must change class inn commons-pool2-2.2.jar?
Updated Answer:
Since at least Spring LDAP 2.3.2 you can now use commons-pool2. Spring LDAP now provides two classes:
For commons-pool 1.x:
org.springframework.ldap.pool.factory.PoolingContextSource
For commons-pool 2.x:
org.springframework.ldap.pool2.factory.PooledContextSource
Details can be found here:
https://github.com/spring-projects/spring-ldap/issues/351#issuecomment-586551591
Original Answer:
Unfortunately Spring-Ldap uses commons-pool and not commons-pool2. As you have found the class org.apache.commons.pool.KeyedPoolableObjectFactory does not exist in commons-pool2 (it has a different package structure), hence the error.
There is a Jira issue for the Spring-ldap project asking them to upgrade/support commons-pool2:
https://jira.spring.io/browse/LDAP-316
Until that has been completed you will have to use commons-pool 1.6.

Usage of CustomEditor with BeanWrapperFieldExtractor just like with BeanWrapperFieldSetMapper

I have written a simple Spring Batch application that reads a CSV file, does some transforming and writes a modified CSV to the disk.
The reading of the file into domain objects works like a charm. I use DelimitedLineTokenizer to tokenize the lines and a BeanWrapperFieldSetMapper to feed the values into a bean:
<bean id="reader" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step">
<property name="resource" value="#{jobParameters['inputResource']}" />
<property name="linesToSkip" value="1" />
<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="delimiter" value=";" />
<property name="names"
value="ID,NAME,DESCRIPTION,PRICE,DATE" />
</bean>
</property>
<property name="fieldSetMapper">
<bean class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
<property name="targetType" value="myapp.MyDomainObject" />
<property name="customEditors">
<map>
<entry key="java.util.Date" value-ref="dateEditor" />
<entry key="java.math.BigDecimal" value-ref="numberEditor" />
</map>
</property>
</bean>
</property>
</bean>
</property>
</bean>
I especially like the features of BeanWrapperFieldSetMapper to "guess" the field names and the possibility to define CustomEditors which I use to define the special date and number formats used in the input file.
Now I would like to write the modified file in the same format like the input file.
I use the following configuration:
<bean id="writer" class="org.springframework.batch.item.file.FlatFileItemWriter" scope="step">
<property name="resource" value="#{jobParameters['outputResource']}" />
<property name="lineAggregator">
<bean class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
<property name="delimiter" value=";" />
<property name="fieldExtractor">
<bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
<property name="names" value="id,name,description,price,date" />
</bean>
</property>
</bean>
</property>
</bean>
There are two things I miss with this configuration:
BeanWrapperFieldSetMapper allowed me to set CustomEditors, but BeanWrapperFieldExtractor has no such possibility. Is there a way to use these?
Is there a way to define the headings in the first line of the file? I have not found any way to write an initial line that is not a bean... It would be great to use the same names here as in BeanWrapperFieldSetMapper such that BeanWrapperFieldExtractor writes the inital line and guesses the bean property namens as BeanWrapperFieldSetMapper does.
The process to load files is so comfortable in Spring Batch. Why is the writing of files so different? Am I missing something?
I have to use Spring Batch 2.1.x because we are using Spring 3.0.x . Therefor an upgrade to 2.2.x would not be an option.
Which is your need? Extract field property as text? You can
use a FormatterLineAggregator if you needs are not too complicated
write your own CustomEditorsFieldExtractor (better)
Generate a complex domain object composed by original domain object and by text-formatted object and use last one as parameter of writer (but breaks your current processor/writer)
Use FlatFileItemWriter.headerCallback: if setted allow custom header write
Writing - in your case - seems a pain respect read process because spring-batch's reading components fits your needs. Standard components fits more used use-case and they cover a lot of scenario. Let us write a custom FieldExtractor sometimes! :)

spring-data embadded-database configuration

I try to configure springdata with embedded-database. Context file inspired from http://cooldevstuff.wordpress.com/2012/09/20/in-memory-database-using-spring-3-2/
My context file:
<jdbc:embedded-database id="embeddedDataSource">
<jdbc:script location="classpath:schemaFile.sql" />
<jdbc:script location="classpath:dataFile.sql" />
</jdbc:embedded-database>
<jpa:repositories base-package="aa.bb.repository" />
<bean class="org.springframework.orm.hibernate4.HibernateExceptionTranslator" />
<bean id="entityManagerFactory">
<property name="dataSource" ref="embeddedDataSource" />
<property name="persistenceUnitName" value="SamplePU"></property>
</bean>
but I get error:
No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 0.
what I missed?
The configuration is just invalid. Your bean entityManagerFactory does not declare any class attribute.