初识Spring框架(二)

实现一个简单的Sring实例

1.搭建Spring环境

  新建web项目,引入jar包

初识Spring框架(二)_第1张图片


2.编写HelloWorld类

package com.array.www.spring;

public class HelloWorld {
	public void show(){
		System.out.println("欢迎学习spring框架");
	}
}

3.配置applicationContext.xml文件



	
	
					
4.写测试类

package com.array.www.spring.test;

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

import com.array.www.spring.HelloWorld;

public class TestDemo {

	public static void main(String[] args) {
		//加载配置文件
		ApplicationContext context = 
				new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
		HelloWorld hw = (HelloWorld) context.getBean("hw");
		hw.show();
	}

}

5.运行结果

初识Spring框架(二)_第2张图片




你可能感兴趣的:(spring框架相关)