首先:写一个接口
package cn.sisy.dao;
public interface UserDao {
public void addUser();
}
然后:一个实现类
public class UserDaoImpl implements UserDao {
public void addUser() {
System.out.println("调用了addUser方法");
}
}
再写:配置文件
<?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-2.5.xsd"> <bean id="userDao" class="cn.sisy.dao.impl.UserDaoImpl"></bean> </beans>
最好:写一个测试类
public class TestUserDao {
private static UserDao userDao;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
try {
ApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml");
userDao = (UserDao) cxt.getBean("userDao");
} catch (RuntimeException e) {
e.printStackTrace();
}
}
@Test public void addUser() {
userDao.addUser();
}
}
代码:附件~~~