1、创建一个Web Project
2、右击项目-->Properties
3、搜索Spring -->Peoject Facets-->在右边找到Spring,打勾并保存
4、测试
4.1 创建个类
package cn.spring.user; /**
*
* @author Dzsom
* @date 2018年3月13日下午11:42:03
* @encoding UTF-8
* @version 1.0
**/ public class User {
private String uname;
private int age;
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
} }
4.2 修改src下的配置文件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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">
<!-- 将对象交给Spring管理 -->
<bean name="user" class="cn.spring.user.User"></bean> </beans>
4.3 创建测试类(@Test需要导入junit包)
package cn.spring.test; import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.spring.user.User; /**
*
* @author Dzsom
* @date 2018年3月14日上午9:35:19
* @encoding UTF-8
* @version 1.0
**/ public class Demo {
@Test
public void fun() {
//1.创建容器对象
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
//2.向容器要对象
User user = (User) ac.getBean("user");
//3.打印
System.out.println(user); }
}
4.4 右击运行测试,若有值输出则Spring搭建成功