Announcement Announcement Module
Collapse
No announcement yet.
Using PropertyOverrideConfigurer to override value of Map property embedded in a bean Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • #16
    Originally posted by amiladomingo View Post
    Is this what you want?

    Following is the bean that has the properties instance variable, it loads the default values in the constructor. Note that in the setter i do this.dbProperties.putAll(dbProperties); not this.dbProperties = dbProperties. I do this to keep old properties that will not get overriden.

    Code:
    public class DbPropsBean {
    
    	private Properties dbProperties = new Properties();
    
    	public DbPropsBean() {
    		dbProperties.put("jdbc.driverclass", "oracle.jdbc.OracleDriver");
    		dbProperties.put("hibernate.sql.dialect", "org.hibernate.dialect.Oracle9Dialect");
    		dbProperties.put("jdbc.url", "jdbc:oracle:thin:@localhost:1521:xe");
    	}
    
    	public Properties getDbProperties() {
    		return dbProperties;
    	}
    
    	public void setDbProperties(Properties dbProperties) {
    		this.dbProperties.putAll(dbProperties);
    	}
    }
    Spring configuration. You can put the properties that you want to override

    Code:
    	<bean
    		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    		<property name="locations" value="classpath:/database.properties" />
    	</bean>
    
    	<bean id="dbProps" class="DbPropsBean">
    		<property name="dbProperties">
    			<props>
    				<prop key="jdbc.url">${jdbc.url}</prop>
    			</props>
    		</property>
    	</bean>
    database.properties file,

    Code:
    jdbc.url=jdbc:oracle:thin:@remote:1521:xe
    Test class,

    Code:
    public class TestClass {
    
    	private static ConfigurableApplicationContext appCtx;
    
    	public static void main(String[] args) {
    		appCtx = new ClassPathXmlApplicationContext(
    				new String[] { "applicationContext.xml" });
    		
    		DbPropsBean bean = (DbPropsBean) appCtx.getBean("dbProps");
    		
    		System.out.println(bean.getDbProperties().getProperty("jdbc.url"));
    		
    	}
    }
    Thanks a lot, Amiladomingo. I was able to overcome the issue.
    There was also a mistake on my part, I had the bean id starting with a UPPER CASE. When I changed it to lowe case, it worked fine.

    properties file : beanName.mapProperty[KEY1]=value1

    XML :

    <bean id="beanName"
    class="com.xx.SomeProcessor"
    scope="prototype">
    <property name="mapProperty">
    <map>
    <entry key="KEY1" value="${beanName.mapProperty.KEY1}"/>
    </map>
    </property>
    </bean>

    Comment


    • #17
      Hey, see if following is useful for you

      Code:
      	<property name="dbProperties">
      			<props merge="true">
      				<prop key="jdbc.url">${jdbc.url}</prop>
      			</props>
      	</property>
      It will merge the properties you have in your java code with the once that is in context you can use the same for map too

      Comment

      Working...
      X