Spring3.0_构建Spring开发环境的小示例

一、添加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的支持环境已经构建完毕。

附:源码下载http://u.115.com/file/bh56mqty

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值