本文共 1398 字,大约阅读时间需要 4 分钟。
1.创建普通的Maven项目
2.导入jar包org.springframework spring-webmvc 4.3.9.RELEASE
我们可以看到,只要导入spring-webmvc这个包,Maven就会自动导入依赖的其他jar包
3.创建pojopackage com.muhan.pojo;public class User { private String name; public User() { } public User(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "User{" + "name='" + name + '\'' + '}'; }}
4.编写Spring的配置文件applicationContext.xml
5.测试
import com.muhan.pojo.User;import org.junit.Test;import org.springframework.context.support.ClassPathXmlApplicationContext;public class UserTest { @Test public void test(){ //获取ClassPathXmlApplicationContext对象,关联spring配置文件applicationContext.xml ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); //获取对象名为user的对象 User user = (User) context.getBean("user"); System.out.println(user); //获取对象名为user2的对象 User user2 = (User) context.getBean("user2"); System.out.println(user2); }}
结果展示:
6.项目结构
转载地址:http://xoiwi.baihongyu.com/