0

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?

user755806
  • 6,565
  • 27
  • 106
  • 153
  • you can make your own config file with all required data – itwasntme Aug 12 '15 at 14:14
  • possible duplicate of http://stackoverflow.com/questions/317687/how-can-i-inject-a-property-value-into-a-spring-bean-which-was-configured-using – cahen Aug 12 '15 at 14:21

2 Answers2

2

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);
}
GreenGiant
  • 4,930
  • 1
  • 46
  • 76
0

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}"/>
Goyal Vicky
  • 1,249
  • 16
  • 16