(1)在pom.xml中引入依赖
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
(2)创建IUserDao和IUserDaoImp
package com.dao;
public interface IUserDao {
public void save();
}
package com.imp;
import com.dao.IUserDao;
public class IUserDaoImp implements IUserDao {
@Override
public void save() {
System.out.println("已经成功保存");
}
}
(3)配置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.xsd">
<!--设置约束头,配置IUserDaoImp-->
<bean id="userDao" class="com.imp.IUserDaoImp"></bean>
</beans>
(4)创建test类,进行测试
package com.test;
import com.dao.IUserDao;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringTest {
@Test
public void test(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
IUserDao userDao = (IUserDao)applicationContext.getBean("userDao");
userDao.save();
}
}