spring面向接口编程

Posted bigsiji

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring面向接口编程相关的知识,希望对你有一定的参考价值。

(1)创建一个接口
package com.min.dao;

public interface UserDao {

    public void save(String uname, String pwd);
    
}
(2)创建一个实现类将用户信息保存到mysql数据库中
package com.min.dao.impl;

import com.min.dao.UserDao;

public class UserDaoMysqlImpl implements UserDao {

    @Override
    public void save(String uname, String pwd) {
        System.out.println("----------UserDaoMysqlImpl----------------");
    }

}
(3)创建一个实现类将用户信息保存到oracle数据库中
package com.min.dao.impl;

import com.min.dao.UserDao;

public class UserDaoOracleImpl implements UserDao{

    @Override
    public void save(String uname, String pwd) {
        System.out.println("----------UserDaoOracleImpl----------------");
    }

}
(4)创建一个管理类,将接口对象作为其属性
package com.min.manager;

import com.min.dao.UserDao;

public class UserManager {

    private UserDao userDao;  //将接口对象作为属性
    
    public void save (String uname, String pwd) {
        userDao.save(uname, pwd);
    }

    public UserDao getUserDao() {
        return userDao;
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
    
}
(5)在Spring配置文件applicationContext.xml将javaBean由Spring管理 
<?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:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
    <bean id="hellword" class="com.min.Hellword">
    <property name="message" value="heel"></property>
    </bean>
    <bean id="mysqlimpl" class="com.min.dao.impl.UserDaoMysqlImpl"></bean>
    <bean id="oracleimpl" class="com.min.dao.impl.UserDaoOracleImpl"></bean>
   <!-- manager的userDao属性依赖注入Spring来注入,可以在程序中无需修改代码,就可以注入不同实例,本例中向mysql保存数据就注入mysqlimpl,如果后来向Oracle中保存数据只需修改注入实例--> <bean id="usermanager" class="com.min.manager.UserManager"> <property name="userDao" ref="mysqlimpl"></property> </bean> </beans>
(6)编写测试类
package com.min.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.min.manager.UserManager;

public class Test {

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserManager manager = (UserManager)context.getBean("usermanager");
        manager.save("user", "pwd ");
    }
}
执行结果
四月 21, 2019 10:46:19 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org[email protected]5f4da5c3: startup date ; root of context hierarchy
四月 21, 2019 10:46:19 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
----------UserDaoMysqlImpl----------------pwd 

 

以上是关于spring面向接口编程的主要内容,如果未能解决你的问题,请参考以下文章

VSCode自定义代码片段——JS中的面向对象编程

VSCode自定义代码片段9——JS中的面向对象编程

Spring学习:面向接口编程思想

spring面向接口编程

面向面试编程代码片段之GC

分享一套spring+springMVC+mybaits+EasyUI+jQuery+Ajax+面向接口编程进销存管理系统源码!