在目录F:\spring下进行操作
1:拷贝jar包
spring.jar
log4j-1.2.9.jar
commons-logging.jar
2:拷贝dtd文件
spring-beans.dtd
3:设置环境变量
set CLASSPATH=.;F:\spring\spring.jar;F:\spring\log4j-1.2.9.jar;F:\spring\commons-logging.jar;%CLASSPATH%
4:编写log4j配置文件
log4j.properties
5:编写java代码
Hello.java
HelloEnglish.java
HelloChinese.java
Main.java
6:编译
javac Hello.java
javac HelloEnglish.java
javac HelloChinese.java
javac Main.java
7:编写spring配置文件beans.xml
8:执行
java Main
修改beans.xml中的 HelloEnglish 为 HelloChinese输出的hello!变为你好!
log4j.properties
# For JBoss: Avoid to setup Log4J outside $JBOSS_HOME/server/default/deploy/log4j.xml!
# For all other servers: Comment out the Log4J listener in web.xml to activate Log4J.
log4j.rootLogger=INFO, stdout, logfile
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=testspring.log
log4j.appender.logfile.MaxFileSize=512KB
# Keep three backup files.
log4j.appender.logfile.MaxBackupIndex=3
# Pattern to output: date priority [category] - message
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
Hello.java
public interface Hello
{
public void hello();
}
HelloEnglish.java
public class HelloEnglish implements Hello
{
public void hello()
{
System.out.println("hello!");
}
}
HelloChinese.java
public class HelloChinese implements Hello
{
public void hello()
{
System.out.println("你好!");
}
}
Main.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main
{
public static void main(String args[])
{
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
Hello hl = (Hello)ctx.getBean("hello");
hl.hello();
return ;
}
}
beans.xml
阅读(1122) | 评论(0) | 转发(0) |