tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Spring > Core > Property Place Holder

Property Place Holder 

Spring Inversion of Control (IoC) also known as Dependency Injection (DI) is a process by which objects define their dependencies with collaborating objects. The following example shows using PropertyPlaceholderConfigurer which allows us to read property values from external configuration file in the standard Java Properties format.

File Name  :  
/SpringCore001/conf/prop/prop_place_holder.xml 

File Name  :  
/SpringCore001/conf/prop/com/btc/app.properties 
my.prop.threadPoolSize=9
my.prop.threadPoolMgrCls=com.btc.test.SimpleThreadPoolManager
my.prop.threadTimeout=120

File Name  :  
com/bethecoder/tutorials/spring3/basic/SimpleApplication.java 
   
package com.bethecoder.tutorials.spring3.basic;

public class SimpleApplication {

  private int threadPoolSize;
  private String threadPoolMgrCls;
  private int threadTimeout;

  public int getThreadPoolSize() {
    return threadPoolSize;
  }
  public void setThreadPoolSize(int threadPoolSize) {
    this.threadPoolSize = threadPoolSize;
  }
  public String getThreadPoolMgrCls() {
    return threadPoolMgrCls;
  }
  public void setThreadPoolMgrCls(String threadPoolMgrCls) {
    this.threadPoolMgrCls = threadPoolMgrCls;
  }
  public int getThreadTimeout() {
    return threadTimeout;
  }
  public void setThreadTimeout(int threadTimeout) {
    this.threadTimeout = threadTimeout;
  }

  public String toString() {
    return "[threadPoolSize = " + threadPoolSize + 
      ", threadPoolMgrCls = " + threadPoolMgrCls + 
      ", threadTimeout = " + threadTimeout + "]";
  }
}
   

File Name  :  
com/bethecoder/tutorials/spring3/tests/property/PropertyPlaceHolderTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.spring3.tests.property;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.bethecoder.tutorials.spring3.basic.SimpleApplication;

public class PropertyPlaceHolderTest {

  /**
   @param args
   */
  public static void main(String[] args) {
    
    System.out.println("Initializing ApplicationContext");
    ApplicationContext factory = new ClassPathXmlApplicationContext("prop_place_holder.xml");
    System.out.println("ApplicationContext Initialized");
    
    SimpleApplication myApp = (SimpleApplicationfactory.getBean("myApp");
    System.out.println(myApp);
  }

}
   

It gives the following output,
Initializing ApplicationContext
ApplicationContext Initialized
[threadPoolSize = 9, threadPoolMgrCls = com.btc.test.SimpleThreadPoolManager, threadTimeout = 120]



 
  


  
bl  br