一、添加Spring的jar支持包
二、添加Spring的核心文件 applicationContext.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.xsd"> <bean id="HelloWorld" class="com.demo.spring.HelloWorld"> <property name="message"><value>world</value></property> </bean> </beans>
三、添加日志配置文件:log4j.properties
## LOGGERS ##
#define a logger
#log4j.rootLogger=DEBUG,console,file
log4j.rootLogger=INFO,console,file
## APPENDERS ##
# define an appender named console, which is set to be a ConsoleAppender
log4j.appender.console=org.apache.log4j.ConsoleAppender
# define an appender named file, which is set to be a RollingFileAppender
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=d:/demo_log.txt
#set the log's size
log4j.appender.file.MaxFileSize=1000KB
log4j.appender.file.MaxBackupIndex=20
## LAYOUTS ##
# assign a SimpleLayout to console appender
log4j.appender.console.layout=org.apache.log4j.SimpleLayout
# assign a PatternLayout to file appender
log4j.appender.file.layout=org.apache.log4j.PatternLayout
# For debug
# log4j.appender.file.layout.ConversionPattern=[%-5p][%t][%C][%d{yyyy-MM-dd HH:mm:ss}] %m%n
# For deployment
log4j.appender.file.layout.ConversionPattern=[%-5p][%d{yyyy-MM-dd HH:mm:ss}] %m%n
项目的目录
com.demo.spring.HelloWorld的代码:
package com.demo.spring;
public class HelloWorld {
protected String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String excute(){
return "Hello"+getMessage();
}
}
com.demo.test.Test的代码:
package com.demo.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import com.demo.spring.HelloWorld;
public class Test {
public static void main(String args[]){
ApplicationContext ctx=new FileSystemXmlApplicationContext(
"WebContent/WEB-INF/applicationContext.xml");
HelloWorld hello=(HelloWorld) ctx.getBean("HelloWorld");
System.out.println(hello.excute());
}
}
输出结果如下:
INFO - Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@4b222f: startup date [Tue Jul 12 00:01:34 CST 2011]; root of context hierarchy
INFO - Loading XML bean definitions from file [D:\My Documents\eclipse workspace\test\WebContent\WEB-INF\applicationContext.xml]
INFO - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1c9b9ca: defining beans [HelloWorld]; root of factory hierarchy
Helloworld
出现Helloworld字样说明Spring的支持环境已经构建完毕。