码迷,mamicode.com
首页 > 编程语言 > 详细

spring面向接口编程

时间:2019-04-21 11:36:12      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:source   import   art   sage   配置文件   res   依赖注入   管理   rop   

(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.springframework.context.support.ClassPathXmlApplicationContext@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面向接口编程

标签:source   import   art   sage   配置文件   res   依赖注入   管理   rop   

原文地址:https://www.cnblogs.com/bigsiji/p/10744258.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!