spring 注入之 xml

一  使用xml注入 xml中进行配置

需要注意:spring容器在 执行代码

ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");时候才进行了初始化


  重点说明:bean name 具有唯一性 通过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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  <bean name="stuDaoImpl"class="com.bjsxt.dao.impl.StudentDaoImpl"> </bean>
  <bean name="student"class="com.bjsxt.model.Student" scope="singleton" lazy-init="true"init-method="init" destroy-method="destroy"></bean>  <!--  prototype 每次生成的对象不同 prototype
                                                                            lazy-init=false 默认在容器进行初始化的时候就初始化了该对象
可以通过测试类来看出如果=true则直到调用时候才会初始化该对象
                                                     -->
  <bean name="studentService"class="com.bjsxt.service.StudentService">
    <property name="studentDao"ref="stuDaoImpl"></property><!-- 普通值用value 对于特定的类的对象则用ref需要注意的是 ref需要在xml文件中进行配置-->
  </bean>
</beans>


二 类图

目录结构如下图所示,在这里我们只测试studentService


Dao层次

packagecom.bjsxt.dao;

importcom.bjsxt.model.*;

public interface StudentDao {

public void StudentSave(Student s);

}

Impl层

packagecom.bjsxt.dao.impl;

importcom.bjsxt.dao.*;

importcom.bjsxt.model.Student;

 

public class StudentDaoImpl implements StudentDao{

 

    @Override

    public void StudentSave(Student s) {

          System.out.println("学生被保存!");

       

    }

}

packagecom.bjsxt.model;

public class Student {

 private int id;

 private Stringname;

public void init()

{

    System.out.println("初始化......");

}

public int getId() {

    return id;

}

public void setId(intid) {

    this.id =id;

}

publicString getName() {

    return name;

}

public void setName(String name) {

    this.name =name;

}

public void destroy()

{

    System.out.println("销毁。。。。");

}

}

packagecom.bjsxt.service;

importcom.bjsxt.dao.*;

import com.bjsxt.dao.impl.*;

importcom.bjsxt.model.*;

public class StudentService {

privateStudentDaostudentDao;

//降低了耦合性:StudentService并不知道具体由谁来保存学生sioc容器来控制装配

 

publicStudentDao getStudentDao() {

    return studentDao;

}

 

public void setStudentDao(StudentDao studentDao) {

    this.studentDao =studentDao;

}

public void add(Student s)

{

    this.studentDao.StudentSave(s);

}

}

测试类

packagecom.bjsxt.service;

 

importorg.junit.Test;

importorg.springframework.context.ApplicationContext;

importorg.springframework.context.support.ClassPathXmlApplicationContext;

importcom.bjsxt.model.Student;

public class StudentServiceTest {

    @Test

    public void test() {

        System.out.println("程序运行之前....");

        ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");

        System.out.println("程序运行开始....");

        StudentService sService=(StudentService)ctx.getBean("studentService");

        Student student=(Student)ctx.getBean("student");

        Student student2=(Student)ctx.getBean("student");

        System.out.println(student2==student);

        sService.add(student);

    }

}



郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。