创建名为springTest1的WEB工程,具体的文件结构图如下所示:
1,将Spring框架所需的JAR包加载到工程中(只需要基本的,这里加载的多了一些),具体如下:
2,在spring包中创建Friend接口,同时创建实现该接口的People类,
Friend接口:
View Code
package spring;public interface Friend { public String sayHello(String s);}
People类:
View Code
package spring;public class People implements Friend { public String sayHello(String s){ return "你好,"+s+",欢迎进入spring的学习!"; }}
3,在web.xml中创建web监听:
View Code
index.jsp org.springframework.web.context.ContextLoaderListener
4,创建配置文件Application.xml:
View Code
5,创建测试类TestSpringApplicationContext:
View Code
package spring;import org.springframework.context.ApplicationContext;import org.springframework.context.support.FileSystemXmlApplicationContext;public class TestSpringApplicationContext { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub ApplicationContext context=new FileSystemXmlApplicationContext("WebRoot/WEB-INF/classes/applicationContext.xml"); Friend friend=(Friend)context.getBean("People"); System.out.println(friend.sayHello("Friend")); }}
6,运行测试类: