一需要的jar包
commons-logging
log4j-1.2.14
spring
二需要自创library然后填入到项目中,具体步骤别的文章已有不再赘述
三整体结构大致如图
client依赖student依赖teacher。
下面是比较重要的部分
MyStudent类
package MyStudent;
import MyTeacher.MyTeacherInterface;
public class StudentImp implements StudentInterface{
private MyTeacherInterface teacher;
@Override
public void addUser(String username, String password) {
// TODO Auto-generated method stub
teacher.addUser(username, password);
}
public void setTeacher(MyTeacherInterface teacher) { ///通过set方式来进行注入
this.teacher = teacher;
}
}
客户端类
BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
StudentInterface student = (StudentInterface)factory.getBean("StudentImp");
student.addUser("李卫", "当官");
重头戏在于xml文件
<bean id="MyTeacherImple" class="MyTeacher.MyTeacherImple" />
<bean id="StudentImp" class="MyStudent.StudentImp">
<property name="teacher" ref="MyTeacherImple" />
</bean>
MyTeacherImple是真实存在的类,class是其路径
StudentImp应该也是真实存在的,client里getbean会用到
teacher应该是student里声明的d层的名字