log4j配置
在src目录下增加
log4j.properties:
1 2 3 4 5 6 7 | log4j.appender.logFile=org.apache.log4j.FileAppender log4j.appender.logFile.Threshold=DEBUG log4j.appender.logFile.ImmediateFlush=true log4j.appender.logFile.Append=true log4j.appender.logFile.File=F:/Workspaces/logs/log.log4j log4j.appender.logFile.layout=org.apache.log4j.PatternLayout log4j.appender.logFile.layout.ConversionPattern=[%-5p] %d(%r) --> [%t] %l\: %m %x %n |
package Acton下
Action.java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | package Action; public interface Action { public String execute(String str); } LowerAction.java: package Action; public class LowerAction implements Action { private String message; public String getMessage() { return message; } public void setMessage(String string) { message = string; } public String execute(String str) { return (getMessage() + str).toLowerCase(); } } |
UpperAction.java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | package Action; public class UpperAction implements Action { private String message; public String getMessage() { return message; } public void setMessage(String string) { message = string; } public String execute(String str) { return ((getMessage() + str).toUpperCase()); } } |
Test.java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | package Action; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; public class Test { public static Log log = LogFactory.getLog( "Test" ); public static void main(String Args[]) { log.info( "Start..." ); // String[] locations = { "beans.xml" }; /*ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); Action action = (Action) ctx.getBean("beanID"); System.out.println(action.execute(" Joson"));*/ Resource resource= new ClassPathResource( "beans.xml" ); @SuppressWarnings ( "deprecation" ) BeanFactory factory= new XmlBeanFactory(resource); Action action=(Action)factory.getBean( "beanID" ); System.out.println(action.execute( " The new future" )); log.info( "End..." ); } } |
beans.xml:
<?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <description>Spring Quick Start</description> <bean id="beanID" class="Action.LowerAction"> <property name="message" value="hello"/> </bean> </beans>
加载Spring和log4j的jar包
作者: lost blog
出处: http://www.cnblogs.com/JAYIT/
关于作者:专注服务器端开发
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接 如有问题, 可邮件(sawyershaw@qq.com)咨询.
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步