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.
my.prop.threadPoolSize=9
my.prop.threadPoolMgrCls=com.btc.test.SimpleThreadPoolManager
my.prop.threadTimeout=120
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 + "]" ;
}
}
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 = ( SimpleApplication ) factory.getBean ( "myApp" ) ;
System.out.println ( myApp ) ;
}
}
It gives the following output,
Initializing ApplicationContext
ApplicationContext Initialized
[threadPoolSize = 9, threadPoolMgrCls = com.btc.test.SimpleThreadPoolManager, threadTimeout = 120]