初学spring3.05,一个helloworld例子

运行环境

  1、myeclpise10.5

 2、JDK6

3、 junit-4.11.jar 、hamcrest-core-1.3.jar 、 hamcrest-library-1.3.jar

4、spring依赖的com.springsource.org.apache.log4j-1.2.15.jar

5、spring3.05

6、

准备需要的jar包

  核心jar包:从下载的spring-framework-3.0.5.RELEASE-with-docs.zip中dist目录查找如下jar包

 
 

org.springframework.asm-3.0.5.RELEASE.jar

org.springframework.core-3.0.5.RELEASE.jar

org.springframework.beans-3.0.5.RELEASE.jar

org.springframework.context-3.0.5.RELEASE.jar

org.springframework.expression-3.0.5.RELEASE.jar

 org.springframework.context.support-3.0.5.RELEASE.jar

 

 

 

 

 

 

 

   

 

  依赖的jar包:从下载的spring-framework-3.0.5.RELEASE-dependencies.zip中查找如下依赖jar包

 
 

com.springsource.org.apache.log4j-1.2.15.jar

com.springsource.org.apache.commons.logging-1.1.1.jar

com.springsource.org.apache.commons.collections-3.2.1.jar









创建的普通JAVA工程目录如下



接口:HelloApi.java

package com.luhy.spring.hello;

public interface HelloApi {
	public void sayHello();
}

实现上述接口的类:HelloImpl.java

package com.luhy.spring.hello;

public class HelloImpl implements HelloApi {
	public void sayHello(){
		System.out.println("hello world...");
	}
}


测试类:HelloTest.java

package com.luhy.spring.hello;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class HelloTest {
	@Test
	public void testHelloWorld(){
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		HelloApi h = context.getBean("hello", HelloApi.class);
		h.sayHello();
	}
	
	
//	public static void main(String[] args) {
//		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//		HelloApi h = context.getBean("hello", HelloApi.class);
//		h.sayHello();
//	}
}
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" 
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="  
http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
http://www.springframework.org/schema/context                http://www.springframework.org/schema/context/spring-context-3.0.xsd">

	<!-- id 表示你这个组件的名字,class表示组件类 -->
	<bean id="hello" class="com.luhy.spring.hello.HelloImpl">
	</bean>
	
 </beans>



log4j.properties
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c:%L - %m%n

### direct messages to file hibernate.log ###
#log4j.appender.file=org.apache.log4j.FileAppender
#log4j.appender.file.File=hibernate.log
#log4j.appender.file.layout=org.apache.log4j.PatternLayout
#log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### set log levels - for more verbose logging change 'info' to 'debug' ###

log4j.rootLogger=warn, stdout


运行结果:

hello world...





更详情的教程

http://jinnianshilongnian.iteye.com/blog/1482071

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值