- 添加 Spring 依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.3.RELEASE</version>
</dependency>
- 准备测试接口(EmpDao,EmpService )和实现类(EmpDaoImpl, EmpServiceImpl)
- 添加 Spring 核心文件 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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="empService" class="com.centim.service.EmpServiceImpl"></bean>
<bean id="empDao" class="com.centim.dao.EmpDaoImpl"></bean>
</beans>
- 测试方法
package com.centim.spring;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.tedu.service.EmpService;
public class TestSpring {
@Test
public void testSpring() {
ApplicationContext ac =
new ClassPathXmlApplicationContext("applicationContext.xml");
EmpService service = (EmpService) ac.getBean("empService");
service.addEmp();
}
}