08_Spring實現action調用service,service調用dao的過程


【工程截圖】

 

【PersonDao.java】

package com.HigginCui.dao;

public interface PersonDao {
    public void savePerson();
}

【PersonDaoImpl.java】

package com.HigginCui.dao;

public class PersonDaoImpl implements PersonDao{
    @Override
    public void savePerson() {
        System.out.println("save Person...");
    }
}

【PersonService.java】

package com.HigginCui.Service;

public interface PersonService {
    public void savePerson();
}

【PersonServiceImpl.java】

package com.HigginCui.Service;

import com.HigginCui.dao.PersonDao;

public class PersonServiceImpl implements PersonService{
    private PersonDao personDao;
    //personDao的set方法
    public void setPersonDao(PersonDao personDao) {
        this.personDao = personDao;
    }
    @Override
    public void savePerson() {
        this.personDao.savePerson();
    }
}

【PersonAction.java】

package com.HigginCui.action;

import com.HigginCui.Service.PersonService;

public class PersonAction {
    private PersonService personService;
    //personService的set方法
    public void setPersonService(PersonService personService) {
        this.personService = personService;
    }
    public String savePerson(){
        this.personService.savePerson();
        return "saveOK...";
    }
}

【applicationContext.xml】

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:aop="http://www.springframework.org/schema/aop"
       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
           http://www.springframework.org/schema/aop 
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
    
    <bean id="personDao" class="com.HigginCui.dao.PersonDaoImpl"></bean>
    
    <bean id="personService" class="com.HigginCui.Service.PersonServiceImpl">
        <property name="personDao">
            <ref bean="personDao"/>
        </property>
    </bean>
    
    <bean id="personAction" class="com.HigginCui.action.PersonAction">
        <property name="personService">
            <ref bean="personService"/>
        </property>
    </bean>
</beans>

【testPerson.java】

package com.HigginCui.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.HigginCui.action.PersonAction;

public class testPerson {
    @Test
    public void test(){
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        PersonAction personAction=(PersonAction) context.getBean("personAction");
        String str=personAction.savePerson();
        System.out.println(str);
    }
}

【運行結果】

save Person...
saveOK...

 

【小結】

實現了完全的面向接口編程,在代碼端沒有必要關心一個接口的實現類是什么。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM