Subscribe

RSS Feed (xml)

Powered By

Skin Design:
Free Blogger Skins

Powered by Blogger

Thursday, November 22, 2007

java code for Logging Bean

/*
Pro Spring
By Rob Harrop
Jan Machacek
ISBN: 1-59059-461-4
Publisher: Apress
*/



///////////////////////////////////////////////////////////////////////////////////////
//File: logging.xml







///////////////////////////////////////////////////////////////////////////////////////
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();
}
}

No comments: