Logging Bean Example
/* Pro Spring By Rob Harrop Jan Machacek ISBN: 1-59059-461-4 Publisher: Apress */ /////////////////////////////////////////////////////////////////////////////////////// //File: logging.xml <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="loggingBean" class="LoggingBean"/> </beans> /////////////////////////////////////////////////////////////////////////////////////// import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.BeanNameAware; public class LoggingBean implements BeanNameAware { private static final Log log = LogFactory.getLog(LoggingBean.class); private String beanName = null; public void setBeanName(String beanName) { this.beanName = beanName; } public void someOperation() { if(log.isInfoEnabled()) { log.info("Bean [" + beanName + "] - someOperation()"); } } } /////////////////////////////////////////////////////////////////////////////////////// import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.FileSystemResource; public class LoggingBeanExample { public static void main(String[] args) { BeanFactory factory = new XmlBeanFactory(new FileSystemResource( "build/logging.xml")); LoggingBean bean = (LoggingBean)factory.getBean("loggingBean"); bean.someOperation(); } }