Saturday, September 12, 2009

Loading Class from Spring property file

File Name: property-class.properties

message.(class) = Message

File Name: LoadClassFromProp.java

import org.springframework.beans.factory.support.BeanDefinitionReader;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.PropertiesBeanDefinitionReader;
import org.springframework.core.io.ClassPathResource;

public class LoadClassFromProp {

public static void main(String[] args) {
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
BeanDefinitionReader reader = new PropertiesBeanDefinitionReader(factory);
reader.loadBeanDefinitions(new ClassPathResource("property-class.properties"));
Message msg = (Message) factory.getBean("message");
System.out.println(msg.getMessage());
msg.setMessage("Good Afternoon");
System.out.println(msg.getMessage());
}

}

class Message
{
private String message = "";
public Message() {
message = "Good Morning..";
}

public void setMessage(String msg)
{
this.message = msg;
}

public String getMessage()
{
return this.message;
}
}

No comments:

Post a Comment