How to Set the argument value for a TreeSet in Spring?
public class Trainer {
String name;
TreeSet<String> batches;
public Trainer(String name, TreeSet<String> batches) {
super();
this.name = name;
this.batches = batches;
}
#Override
public String toString() {
StringBuilder x=new StringBuilder();
x.append("trainer:").append(name).append("\n");
x.append("batches:\n");
for(String a :batches)
{
x.append(a).append("\n");
}
return x.toString();
}
}
//here is the configuration file
<beans>
<bean id="abc" class="Trainer">
<constructor-arg value="asfsad"/>
<constructor-arg>
<set>
<value>kasdaskdnas</value>
<value>sjbdlsas;dkas</value>
</set>
</constructor-arg>
</bean>
</beans>
This throws an exception when trying to Create a object for it,could not convert constructor argument value of type [java.util.LinkedHashSet] to required type [java.util.TreeSet]:
You can use the util:set tag
<beans>
<bean id="abc" class="Trainer">
<constructor-arg value="asfsad" />
<constructor-arg>
<util:set set-class="java.util.TreeSet">
<value>kasdaskdnas</value>
<value>sjbdlsas;dkas</value>
</util:set>
</constructor-arg>
</bean>
</beans>
You can control the implementation of Set to be used for your Sring-managed set using the set-class attribute.
Something like this: <set set-class="java.util.TreeSet">
Related
public enum TypeEnum {
TYPE1, TYPE2, TYPE3;
}
public class Resources {
List<String> suppliers;
EnumMap<TypeEnum, String> items;
//setter and getter
}
I'm in the process of replicating the above bean in spring config xml file. I'm trying as per below but struck up with EnumMap. I understand that we can try using util:map. Please help.
<bean name="products" class="com.company.xxxx.Resources">
<property name="suppliers">
<list>
<value>suppliers1</value>
<value>suppliers2</value>
<value>suppliers3</value>
</list>
</property>
//TODO
<util:map id="items" >
</util:map>
</bean>
I have an object returned from item processor.
public class PcdRateMapper
{
private Pcdrate pcdRate;
private Boolean isValidPcdRate;
public PcdRateMapper ()
{
// pcdRate = new Pcdrate ();
}
public Pcdrate getPcdRate ()
{
return pcdRate;
}
public void setPcdRate (Pcdrate pcdRate)
{
this.pcdRate = pcdRate;
}
public Boolean getIsValidPcdRate ()
{
return isValidPcdRate;
}
public void setIsValidPcdRate (Boolean isValidPcdRate)
{
this.isValidPcdRate = isValidPcdRate;
}
Now i want to extract only Pcdrate object values in my item writer. How can I do this. Currently I'm using following spring configuration but getting invalid property exception. Thanks in advance.
<
property name="lineAggregator">
<bean
class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
<property name="delimiter" value="," />
<property name=""></property>
<property name="fieldExtractor">
<bean
class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
<property name=""></property>
<property name="names"
value="company, subcoy" />
</bean>
</property>
</bean>
</property>
The invalid property exception may stem from
<property name=""></property>
where the property name is an empty string. You have that twice in the code above, remove it.
Your xml structure seems to be invalid, see spring_bean_definition
to see how it should look like.
On the bean of type BeanWrapperFieldExtractor you must set the property 'names' to the names of properties that you want to extraxt, in your case 'pcdRate'.
It should be configured like this :
<bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
<property name="names" value="pcdRate" />
</bean>
I am using spring. I need to return object based on the String. I have below code.
public class DaoFactoryImpl implements DaoFactory {
private String dbType;
private OrganizationActions organizationActions;
private ProductActions productActions;
public void setOrganizationActions(OrganizationActions org){
this.organizationActions = org;
}
public void setProductActions(ProductActions prodActions){
this.productActions = prodActions;
}
public void setDbType(String dbType){
this.dbType = dbType;
}
#Override
public OrganizationActions getDaoObject() {
if(dbType.equalsIgnoreCase("Oracle")){
return organizationActions;
}else if(dbType.equalsIgnoreCase("DB2")){
return productActions;
}
return null;
}
}
Spring_congig.xml:
<util:properties id="configProps"
location="classpath:config/config.properties" />
<bean id="orgService" class="com.sample.OrganizationMongoService">
</bean>
<bean id="productService" class="com.sample.ProductMongoService"/>
<bean id="daoFactory" class="com.sample.factory.DaoFactoryImpl">
<property name="dbType" value="${dbName}"/>
<property name="organizationActions" ref="orgService"/>
<property name="productActions" ref="productService"/>
</bean>
I specify dbName in config.properties file. I have hard coded the same dbName (Oracle, DB2) in DaoFactoryImpl class. How can I avoid hard coding Oracle, DB2 in the code. Is there anyway to specify this criteria in the spring xml file?
Try creating a map in your spring config and use it to look up the correct instance. For example:
<bean id="daoFactory" class="com.sample.factory.DaoFactoryImpl">
<property name="dbType" value="${dbName}"/>
<property name="typeMap">
<map>
<entry key="Oracle" value-ref="orgService"/>
<entry key="DB2" value-ref="productService"/>
</map>
<property>
</bean>
Then do a lookup in your factory method:
public void setTypeMap(Map<String,Actions> typeMap){
this.typeMap = typeMap;
}
#Override
public OrganizationActions getDaoObject() {
return typeMap.get(dbType);
}
You can add the below code in Spring_congig.xml:-
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>properties/database.properties</value>
</property>
</bean>
and define your key-value pair in database.properties as:-
dbName=Oracle
Your Spring_congig.xml will pick up the required value for the given key.
<property name="dbType" value="${dbName}"/>
Hi all i am a beginner for spring i just started it.
i am getting an error
" Error creating bean with name 'question' defined in class path resource [org/collection/ApplicationContext2.xml]: 3 constructor arguments specified but no matching constructor found in bean 'question' (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)"
i have two class 1st is Question that contains a single constructor Question and the second class is Answer
i am trying to create reference of answer class and insert into the Question class which have Array List
i goggled it and found that i need to specify the type.
i have already specified it but still i am getting the error
Thanks..
Question.java
package org.collection;
import java.awt.List;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
public class Question {
private int id;
private String name;
private ArrayList<String> answers;
//private HashSet<String> answers1;
public Question()
{
//Default constructor
}
public Question(int id,String name,ArrayList<String> answers)
{
super();
this.id=id;
this.name=name;
this.answers=answers;
}
public void display()
{
System.out.println("Id :"+id+"\nName :"+name);
System.out.println("Answers are");
Iterator<String> itr= answers.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
/*System.out.println("----------picking up the answers from HashSet---------");
Iterator<String> itr1=answers1.iterator();
while(itr1.hasNext())
{
System.out.println(itr1.next());
}
System.out.println("-------reached-----------");*/
}
}
Answer.java
package org.collection;
public class Answer {
private int id;
private String name;
private String by;
public Answer() {
// TODO Auto-generated constructor stub
}
public Answer(int id,String name,String by)
{
super();
this.id=id;
this.name=name;
this.by=by;
}
public String toString()
{
return "ID :"+id+"\nName"+name+"\nBy :"+by;
}
}
ApplicationContext.xml2
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="ans1" class="org.collection.Answer">
<constructor-arg value="1" type="int"></constructor-arg>
<constructor-arg value="java is a progamming language hahahaha" type="java.lang.String"></constructor-arg>
<constructor-arg value ="varun" type="java.lang.String"> </constructor-arg>
</bean>
<bean id ="ans2" class="org.collection.Answer">
<constructor-arg value="2" type="int"></constructor-arg>
<constructor-arg value="java is a platfornm" type="java.lang.String"></constructor-arg>
<constructor-arg value ="Rahul" type="java.lang.String"></constructor-arg>
</bean>
<bean id="question" class= "org.collection.Question">
<constructor-arg value="111" type="int"></constructor-arg>
<constructor-arg value="What is java ?" type="java.lang.String"></constructor-arg>
<constructor-arg>
<list>
<ref bean="ans1"/>
<ref bean="ans2"/>
</list>
</constructor-arg>
</bean>
</beans>
The three parameter constructor in Question is expecting List of String. But, you are passing List of Answer. Change the third parameter in Question class to ArrayList<Answer> answers
I have a class A which contain the following
class A
{
private HashSet<Long> at = new HashSet<Long>();
and it has a constructor like this
A()
{
//set is being initialsised here
this.at.add(new Long(44));
this.at.add(new Long(34));
this.at.add(new Long(54));
this.at.add(new Long(55));
}
now please advise how can I initialize this hashset of Long type through Spring,,
I have tried ...
<property name="at">
<util:set set-class="java.util.HashSet">
<value>45</value>
<value>65</value>
<value>87</value>
</util:set>
</property>
private HashSet<Long> at;
And try this (make sure you have setter for at)
<bean class="path.A">
<property name="at">
<set>
<value>44</value>
<value>34</value>
<value>54</value>
<value>55</value>
</set>
</property>
</bean>