用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字

猎人薛    -  云代码空间

——

第一个spring简单实例

2015-03-04|1325阅||

摘要:一需要的jar包 commons-logging log4j-1.2.14 spring 二需要自创library然后填入到项目中,具体步骤别的文章已有不再赘述 三整体结构大致如图 client依赖student依赖teacher。 下面是比较重要的部分 MyStudent类 p

一需要的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层的名字




顶 0踩 0收藏
文章评论
    发表评论

    个人资料

    • 昵称: 猎人薛
    • 等级: 中级程序员
    • 积分: 394
    • 代码: 17 个
    • 文章: 4 篇
    • 随想: 0 条
    • 访问: 7 次
    • 关注

    最新提问

      站长推荐