Spring IOC例子詳解

  1. application.xml配置文件java

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
 xmlns:context="http://www.springframework.org/schema/context" 
 xsi:schemaLocation="http://www.springframework.org/schema/beans 
 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
     <!-- 開啓註解 -->
     <context:annotation-config/>
     <bean id="student" class="com.jd.vo.Student">
         <constructor-arg index="0" value="S0001"></constructor-arg>
         <constructor-arg index="1" value="張飛"></constructor-arg>
         <constructor-arg index="2" value="29"></constructor-arg>
     </bean>
     <bean id="studentDao" class="com.jd.dao.impl.StudentDaoImpl"></bean>
     <bean id="studentService" class="com.jd.service.StudentService">
         <!-- setter注入 -->
         <!-- <property name="sDao" ref="studentDao"></property>
         <property name="stu" ref="student"></property> -->
         <!-- 構造方法注入 -->
         <!-- <constructor-arg index="0" ref="studentDao"></constructor-arg>
         <constructor-arg index="1" ref="student"></constructor-arg> -->
     </bean>
</beans>

三種注入方式的配置都寫入了配置文件,註釋掉的是setter和構造方法的注入。spring

2. Student類app

package com.jd.vo;

public class Student {
    
    private String id;
    
    private String name;
    
    private int age;
    
    public Student(String id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

3. StudentDao接口測試

package com.jd.dao;

import com.jd.vo.Student;

public interface StudentDao {
    
    public boolean save(Student stu);
}

4. StudentDaoImpl實現類this

package com.jd.dao.impl;

import com.jd.dao.StudentDao;
import com.jd.vo.Student;

public class StudentDaoImpl implements StudentDao{
    
    public boolean save(Student stu) {
        if(stu == null)
            return false;
        else{
            System.out.println("id : " + stu.getId());
            System.out.println("name : " + stu.getName());
            System.out.println("age : " + stu.getAge());
            return true;
        }
    }
}

5. StudentService類code

package com.jd.service;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.jd.dao.StudentDao;
import com.jd.vo.Student;

public class StudentService {
    
    @Resource(name="studentDao")
    private StudentDao sDao;
    @Autowired
    @Qualifier(value="student")
    private Student stu;
    //此處必須提供默認的構造方法
    public StudentService(){
        
    }
    //構造方法注入
    public StudentService(StudentDao sDao, Student stu) {
        this.sDao = sDao;
        this.stu = stu;
    }

    
    public StudentDao getsDao() {
        return sDao;
    }
    /**
     * setter注入
     * @param sDao
     */
    public void setsDao(StudentDao sDao) {
        this.sDao = sDao;
    }

    public Student getStu() {
        return stu;
    }
    /**
     * setter注入
     * @param stu
     */
    public void setStu(Student stu) {
        this.stu = stu;
    }

    public void save(){
        sDao.save(stu); 
    }
}

6. 測試類xml

public class testSpringIoc extends TestCase{
    public void test(){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("application.xml");
        StudentService s = (StudentService)ctx.getBean("studentService");
        s.save();
    }
}

7. 執行測試類,輸出結果爲接口

id : S0001
name : 張飛
age : 29get

不管採用哪一種注入方式,原理均利用java的反射機制,推薦使用註解的方式,這樣能夠下降Spring的配置。
io

相關文章
相關標籤/搜索