运行环境
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包
|
依赖的jar包:从下载的spring-framework-3.0.5.RELEASE-dependencies.zip中查找如下依赖jar包
|
创建的普通JAVA工程目录如下
接口:HelloApi.java
package com.luhy.spring.hello; public interface HelloApi { public void sayHello(); }
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.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