Spring学习(二)

来源:互联网 发布:java template 编辑:程序博客网 时间:2024/06/05 17:32

为什么要是用Spring依赖注入:
平常的java开发中,程序员在某个类中需要依赖其它类的方法,则通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理,spring提出了依赖注入的思想,即依赖类不由程序员实例化,而是通过spring容器帮我们new指定实例并且将实例注入到需要该对象的类中。依赖注入的另一种说法是“控制反转”,通俗的理解是:平常我们new一个实例,这个实例的控制权是我们程序员,而控制反转是指new实例工作不由我们程序员来做而是交给spring容器来做

关于Spring的属性注入一共有两种方式,分别为XML方式与注解方式,其中XML方式又分为Set注入与构造方法注入,注解方式分为java注解与Spring注解

一、使用属性注入

package com.shuxing.bean;public interface StudentDao {    public boolean saveStudent(Student student);}
package com.shuxing.bean;public class StudentDaoImpl implements StudentDao{    @Override    public boolean saveStudent(Student student) {        if(student!=null)        {            System.out.println("学号:"+student.getPersonid());            System.out.println("名字: "+student.getName());            System.out.println("年龄:"+student.getAge());            return true;        }        else            return false;    }}
package com.shuxing.bean;public interface StudentService {    public boolean saveStudent();}
package com.shuxing.bean;public class StudentServiceImpl implements StudentService{    private StudentDao studentDao;    private Student student;    public StudentServiceImpl() {       }    public StudentServiceImpl(StudentDao studentDao) {        this.studentDao = studentDao;    }    @Override    public boolean saveStudent() {        if(studentDao.saveStudent(student)) {            return true;        }        else            return false;       }    public StudentDao getStudentDao() {        return studentDao;    }    public void setStudentDao(StudentDao studentDao) {        this.studentDao = studentDao;    }    public Student getStudent() {        return student;    }    public void setStudent(Student student) {        this.student = student;    }}

测试类

package com.shuxing.bean;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestSaveStudent {    public static void main(String[] args) {        //读取配置文件        ApplicationContext ctx=new                 ClassPathXmlApplicationContext("springXML/beans.xml");        Student student=(Student)ctx.getBean("student");        student.setPersonid("1001");        student.setName("zhangsan");        student.setAge(20);        //获取对象        StudentServiceImpl service=(StudentServiceImpl)ctx.getBean("studentService");        if(service.saveStudent()) {            System.out.println("操作成功");        }        else {            System.out.println("操作失败");        }    }}

二、构造方法注入

package com.shuxing.bean;public class StudentServiceImpl implements StudentService{    private StudentDao studentDao;    private Student student;    public StudentServiceImpl() {       }    public StudentServiceImpl(StudentDao studentDao, Student student) {        this.studentDao = studentDao;        this.student = student;    }    @Override    public boolean saveStudent() {        if(studentDao.saveStudent(student)) {            return true;        }        else            return false;       }    /*    public StudentDao getStudentDao() {        return studentDao;    }    public void setStudentDao(StudentDao studentDao) {        this.studentDao = studentDao;    }    public Student getStudent() {        return student;    }    public void setStudent(Student student) {        this.student = student;    }    */}

bean.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-3.0.xsd">    <bean id="helloWorld" class="com.firstSpring.HelloWorldImpl"></bean>    <bean name="student" class="com.shuxing.bean.Student"></bean>    <bean name="studentDao" class="com.shuxing.bean.StudentDaoImpl"></bean>    <bean name="studentService" class="com.shuxing.bean.StudentServiceImpl">        <!-- 1、Setter属性注入 -->          <!-- <property name="属性名称" ref="bean引用名称"></property> -->         <!--          <property name="studentDao" ref="studentDao"></property>        <property name="student" ref="student"></property>        -->        <!-- 2、构造方法注入 -->          <!-- <constructor-arg ref="bean引用名称" index="属性索引"></constructor-arg> -->          <constructor-arg ref="studentDao" index="0"></constructor-arg>          <constructor-arg ref="student" index="1"></constructor-arg>    </bean> </beans>