How to get set elements in the same sequence as in a Spring bean definition - spring

I have a lot of bean definitions that look similar to this
<bean id="TransformationMapOrganization " class="com.artifact_software.adt.plugin.transformation.RemoveColumnsTransformationImpl">
<property name="pluginId" value="Remove Division, Department, and Cost Code" />
<property name="dataStoreName" value="person_data"/>
<property name="columnNames">
<set>
<value>Division</value>
<value>Dept Code</value>
<value>Cost Code</value>
</set>
</property>
</bean>
In the code, the columnNames are defined as:
protected List<String> columnNames;
It appears that erroneous duplicate values are ignored rather causing an error which is good. I hope that I can count on that since it does make life easier!
What set implementation will Spring use?
What is the correct way to iterate through columnNames to get the columnNames in the same sequence as they are specified in the bean?

You can set the implementation class via <set set-class="com.my.SetImpl" />, (see current doc). (com.my.SetImpl must implement java.util.Set)
alternatively: define targetClass on your SetFactoryBean...
If omit, current spring, will use java.util.LinkedHashSet.
More correct, reliable & future-safe it would be to map columnNames as java.util.Set not as a List (+ to use set-class).
If no set-class attribute is supplied, the container chooses a Set implementation.
(in your case,) Obviously spring manages to convert from set to list "smoothly" ("by hand" it's also easy done thx to api design). Spring (seems to) also preserves you distinct entries, LinkedHashSet implementation additionally guarantees/should "preserve order"...

Your Bean definition and field names are different. Change
<property name="ColumnNames">
to
<property name="columnNames">

Related

how to control the sequence of object creation in spring?

in xml based approach, we configure the bean definition in xml ,
beans will be created in the order we have defined the beans.
1) <beans>
<bean id="a" class="com.abc.a"/>
`<bean id="b" class="com.abc.b"/>`
</beans>
Here , a will be created first before b.
2)<beans>
<bean id="a" class="com.abc.a">
<property name="c" ref="c"/>
</bean>
<bean id="b" class="com.abc.b/">
<bean id="c" class="com.abc.c/">
here c will be created first, then a then b.
In case of annotation driven approach, how to control the sequence of object creation? using ordered interface ?
Spring container creates the dependent objects (because they are needed by the main objects as per the object graph) first both in xml & annotation approach.
In case of annotation driven approach, how to control the sequence of object creation? using ordered interface ?
You can't control the order of objects as the dependent objects are always needed to be created first and then followed by the main objects.
The order interface is for a different purpose which is to push the objects into a list using autowired.
You can refer the example in the below link for using #Order to set/push an object into a list:
What is the use of #Order annotation in Spring?
Spring has an Order attribute of java config and an order attribute for xml configuration to control the order in which beans are created. (Lower values means earlyer creation, negative number are allowed too)
An other way is to control the order is DependsOn annotation/attribute.

Synchronized map in spring configuration .1.2.9

We are using Spring 1.2.9 and we are not able to use map:util
Here is the constructor of code which i have to unit test,
public ViewAction() {
screen = Collections.synchronizedMap(new HashMap());
tab = Collections.synchronizedMap(new HashMap());
}
How can i inject the hashmap and add values to the hashmap in the configuration xml.
NOTE : THE QUESTION IS NOT ABOUT USING HASHMAP. IT IS ABOUT CONFIGURING THE xml file. I tried the following and failed
<bean name="viewactionbean" class="com.test.helper.web.ViewAction">
<property name="screen">
<map>
</map>
</property>
</bean>
Error while i configure the above XML file is,
BeanCreationException: Error creating bean with name 'viewactionbean' defined in class path resource
NOTE : Since i am using Spring 1.2.9, i am not able to use "map:util"
You can solve your problem in two different ways:
You can use synchronized(screen) blocks everywhere you need to access anything in the screen map. That way you don't really need a synchronizedMap as you are already guarding your code anytime you access it.
You can set the synchronizedMap in the setter of your bean, so when Spring injects it you are placing a wrapper around it instead the actual instance provided by Spring:
Example setter method:
public void setScreen(Map screen) {
this.screen = Collections.synchronizedMap(screen);
}
Problem with this last approach is that if you need to perform two or more operations in the map inside a single method in your bean, you still need the synchronized(screen) block to protect from race conditions.
To configure the map in spring you should be able to do this:
<bean id="..." class="....">
<property name="screen">
<map>
<entry key="myKey" value="myValue" />
</map>
</property>
</bean>
You have to use something like this for HashMap.
private static Map<K,V> screen = Collections.synchronizedMap(new HashMap<K,V>);
Than for Thread safety you can use .
synchronized (screen) {
}

Spring Configuration Query

I have one spring configuration file with entry like below...
<bean id="beanId" class="a.b.c.d.MyBean">
<property name="firstProperty" value="report_{date}.xls"/>
</bean>
Somewhere in my java code, I am fetching this bean and then its property "firstProperty" later.
I am little curious, when I get the value of property "firstProperty" I get report_.xls i.e report_20130307.xls
I have searched all my code including bundles, xmls but not clear that where we are setting {date} with todays timestamp.
Do you have any clue where we can do this?
Thanks
Jai
It is the property-placeholder mechanism.
Read more on http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/xsd-config.html#xsd-config-body-schemas-context-pphc.
In most of the cases, the values to property are set from properties file using expression language. Like
<bean id="dataSource" class="a.b.c.d.DataSource">
<property name="databaseUrl" value="{db.url}"/>
</bean>
Or if the property is a ref to another bean, e.g. Object B is member variable of Object A.
<bean id="refA" class="a.b.c.d.A">
<property name="b" ref="refB"/>
</bean>
<bean id="refB" class="a.b.c.d.B">
</bean>
Its quite simple guys...as we know setter are called for each property. So same in my case,
In bean we are setting variable "firstProperty" + today timestamp like below.
public void setfirstProperty(String firstProperty) {
this.firstProperty = firstProperty + <methodToReplaceDateStringWithTimeStamp>;
}
Thanks
Jai

c3p0 useScatteredAquireTask Spring Bean

Hello I wan't to know how can I set up a bean so that it sets the ScatteredAquireTask to "True".
I've been trying:
<bean id="c3p0Props" class="com.mchange.v2.resourcepool.BasicResourcePool.ScatteredAcquireTask" >
<property name="USE_SCATTTERED_ACQUIRE_TASK" value="true" />
</bean>
I also tried ...resourcepool.experimental.useScatteredAcquireTask... didn't worked. I'm not sure how can I set this on spring. I'm using 0.9.1.2, can't go to 0.9.2.prep1 at the moment. Thanks.
That's because USE_SCATTTERED_ACQUIRE_TASK isn't a property of the ScatteredAcquireTask class (i.e. there's no method called setUSE_SCATTTERED_ACQUIRE_TASK), it's an internal static field of the class that's not accessible to Spring.
You're not going to be able to set that values in a Spring bean defintion, you need to find out how to influence that value by some other means.

Dynamically configuring java beans based on property file in Spring

Wondering if there is a way to dynamically instantiate beans based on set of values in your property file using PropertyPlaceholderConfigurer class.
I have a java bean say Student with two attributes: "name" and "subject"
I have a property file with:
student.1.name=student1name
student.1.subject=student1subject
student.2.name=student2name
student.2.name=student2subject
Now I have a Classroom object that can take a list of students.
I am wondering if there is a way we could do this using Spring. The challenge here is that the number of students could vary.
If there was only one student object then:
<bean id="student" class="com.abc.Student">
<property name="name" value="${student.1.name}" />
<property name="subject"
value="${student.1.subject}" />
</bean>
<bean id="classRoom" class="com.abc.ClassRoom">
<property name="student" ref="student" />
</bean>
would have worked. But in this case we have a list of n Students. And the value of n could vary depending on the number of entries in the properties file.
I'm with Kevin--IMO you're going about this the wrong way.
One possible workaround would be to create a bean that takes the property file as an argument, reads it in, and exposes a list of students (which would need to be indexed on something, like the n in the existing property file).
The classroom bean could then use that list of students.
But it sure looks like you're trying to duplicate the functionality of a DB, without a DB, in an awkward way.
I don't think there's a way to do that with PropertyPlaceholderConfigurer. Usually when I have a situation like that I choose a configuration format of either JSON or XML and use GSON/Jackson/JAXB to unmarshall the data into objects.

Resources