2. 修改单元测试 JUnit 版本UTF-8 1.8 1.8
3.添加 Spring 框架的依赖坐标junit junit 4.11 test
Maven仓库:https://mvnrepository.com/
org.springframework
spring-context
5.2.4.RELEASE
4.删除build标签中的pluginManagement标签
编写 Bean 对象 UserService
package com.xxxx.service;
public class UserService {
public void test(){
System.out.println("UserService...");
}
}
UserDao
package com.xxxx.dao;
public class UserDao {
public void test(){
System.out.println("UserDao...");
}
}
添加Spring 配置文件
1.在项目的src的main下创建文件夹 resources
2.将 resources 标记为资源目录
3.在 srcmainresources 目录下新建 spring.xml 文件,并拷贝官网文档提供的模板内容到 xml 中。
spring.xml
在 spring.xml 中配置 Bean 对象
配置 bean 到 xml 中,把对应 bean 纳入到 Spring 容器来管理.
加载配置文件,获取实例化对象
package com.xxxx;
import com.xxxx.dao.UserDao;
import com.xxxx.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Starter {
public static void main(String[] args) {
//UserService userService = new UserService();
//userService.test();
//得到spring的上下文环境
ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
//得到对应的实例化对象
UserService userService = (UserService) ac.getBean("userService");
userService.test();
UserDao userDao = (UserDao) ac.getBean("userDao");
userDao.test();
}
}



