zoukankan      html  css  js  c++  java
  • 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...

    【小结】

    实现了完全的面向接口编程,在代码端没有必要关心一个接口的实现类是什么。

  • 相关阅读:
    由于可能不会将凭据发送到远程计算机,因此将不会进行连接。若要获得协助,请与您的系统管理员联系。
    URL编码与解码
    linux服务器openjdk11环境easypoi导出excel报错(class sun.font.CompositeFont cannot be cast to class sun.font.PhysicalFont)
    解决Drools中文乱码
    CISSP-什么是安全冠军以及您为什么需要安全冠军
    windows环境中JDK环境变量配置
    【Django】定时任务
    【Django】权限之has_perm
    一些连接
    数据库
  • 原文地址:https://www.cnblogs.com/HigginCui/p/5574140.html
Copyright © 2011-2022 走看看