Spring Hello World 实例

运行环境

  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

[java]  view plain copy
  1. package com.luhy.spring.hello;  
  2.   
  3. public interface HelloApi {  
  4.     public void sayHello();  
  5. }  

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

[java]  view plain copy
  1. package com.luhy.spring.hello;  
  2.   
  3. public class HelloImpl implements HelloApi {  
  4.     public void sayHello(){  
  5.         System.out.println("hello world...");  
  6.     }  
  7. }  


测试类:HelloTest.java

[java]  view plain copy
  1. package com.luhy.spring.hello;  
  2.   
  3. import org.junit.Test;  
  4. import org.springframework.context.ApplicationContext;  
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  6.   
  7. public class HelloTest {  
  8.     @Test  
  9.     public void testHelloWorld(){  
  10.         ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");  
  11.         HelloApi h = context.getBean("hello", HelloApi.class);  
  12.         h.sayHello();  
  13.     }  
  14.       
  15.       
  16. //  public static void main(String[] args) {  
  17. //      ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");  
  18. //      HelloApi h = context.getBean("hello", HelloApi.class);  
  19. //      h.sayHello();  
  20. //  }  
  21. }  
applicationContext.xml

 

[html]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xsi:schemaLocation="    
  6. http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
  7. http://www.springframework.org/schema/context                http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
  8.   
  9.     <!-- id 表示你这个组件的名字,class表示组件类 -->  
  10.     <bean id="hello" class="com.luhy.spring.hello.HelloImpl">  
  11.     </bean>  
  12.       
  13.  </beans>  



log4j.properties
[html]  view plain copy
  1. log4j.appender.stdout=org.apache.log4j.ConsoleAppender  
  2. log4j.appender.stdout.Target=System.out  
  3. log4j.appender.stdout.layout=org.apache.log4j.PatternLayout  
  4. log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c:%L - %m%n  
  5.   
  6. ### direct messages to file hibernate.log ###  
  7. #log4j.appender.file=org.apache.log4j.FileAppender  
  8. #log4j.appender.file.File=hibernate.log  
  9. #log4j.appender.file.layout=org.apache.log4j.PatternLayout  
  10. #log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n  
  11.   
  12. ### set log levels - for more verbose logging change 'info' to 'debug' ###  
  13.   
  14. log4j.rootLogger=warn, stdout  


运行结果:

[java]  view plain copy
  1. hello world...  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值