导入spring的包就不用说了吧,全世界都知道,不然会找不到类的。
1.建立包first.dao
首先创建一个接口StudentDao,内容如下:
package first.dao;
public interface StudentDao {
void update();
void delete();
}
然后再创建该接口的实现类StudentDaoImpl,内容如下:
package first.dao;
public class StudentDaoImpl implements StudentDao {
public void delete() {
System.out.println("invoke delete");
}
public void update() {
System.out.println("invoke update");
}
}
2.建立包first.logic
首先创建一个接口StudentManager,内容如下:
package first.logic;
public interface StudentManager {
void delete();
void update();
}
然后创建接口的实现类
package first.logic;
import first.dao.StudentDao;
import first.dao.StudentDaoImpl;
public class StudentManagerImple implements StudentManager {
private StudentDao dao;
//dao的SET方法
public void setDao(StudentDao dao) {
this.dao = dao;
}
public void delete() {
dao.delete();
}
public void update() {
dao.update();
}
}
3.在根目录下(src下)建立一个xml文件,名字假设是beans.xml,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd" >
<beans>
<bean id="studentDao" class="first.dao.StudentDaoImpl"></bean>
<bean id="studentLogic" class="first.logic.StudentManagerImple">
<property name="dao">
<ref bean="studentDao"/>
</property>
</bean>
</beans>
4.测试类
package first.view;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import first.logic.StudentManager;
import first.logic.StudentManagerImple;
public class Test {
public static void main(String[] args) {
//StudentManager manager = new StudentManagerImple();
Resource resource =
new ClassPathResource("beans.xml");
BeanFactory factory =
new XmlBeanFactory(resource);
StudentManager manager =
(StudentManager) factory.getBean("studentLogic");
manager.delete();
manager.update();
}
}
这是第一节所学的东东,虽然还不是很清楚,但是你的任务是把这个程序结果跑出来就OK了,愿大家每天都进步一点点,找个好工作。