统计
  • 建站日期:2021-03-10
  • 文章总数:518 篇
  • 评论总数:151 条
  • 分类总数:32 个
  • 最后更新:4月20日
文章 Spring

Spring框架的第一天代码用BeanFactory工厂模式来解耦总结--》没有spring框架,只是思想差不多

梦幻书涯
首页 Spring 正文

/*

 
程序的耦合

       耦合:程序间的依赖关系

 
          包括:

               类之间的依赖

 
              方法间的依赖

       解耦:

 
          降低程序间的依赖关系

       实际开发中:

 
          应该做到:编译期不依赖,运行时才依赖。

       解耦的思路:

 
          第一步:使用反射来创建对象,而避免使用new关键字。

           第二步:通过读取配置文件来获取要创建的对象全限定类名

 


 /

public class JdbcDemo1 {

    public static void main(String[] args) throws  Exception{

        //1.注册驱动

//        DriverManager.registerDriver(new com.mysql.jdbc.Driver());

        Class.forName("com.mysql.jdbc.Driver");



        //2.获取连接

        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/eesy","root","1234");

        //3.获取操作数据库的预处理对象

        PreparedStatement pstm = conn.prepareStatement("select
from account");

        //4.执行SQL,得到结果集

        ResultSet rs = pstm.executeQuery();

        //5.遍历结果集

        while(rs.next()){

            System.out.println(rs.getString("name"));

        }

        //6.释放资源

        rs.close();

        pstm.close();

        conn.close();

    }


手写工厂模式:

bean.properties内容:

          accountService=com.itheima.service.impl.AccountServiceImpl
          accountDao=com.itheima.dao.impl.AccountDaoImpl


工厂类的书写:


package com.itheima.factory;

import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * 一个创建Bean对象的工厂
 *
 * Bean:在计算机英语中,有可重用组件的含义。
 * JavaBean:用java语言编写的可重用组件。
 *      javabean >  实体类
 *
 *   它就是创建我们的service和dao对象的。
 *
 *   第一个:需要一个配置文件来配置我们的service和dao
 *           配置的内容:唯一标识=全限定类名(key=value)
 *   第二个:通过读取配置文件中配置的内容,反射创建对象
 *
 *   我的配置文件可以是xml也可以是properties
 */
public class BeanFactory {
    //定义一个Properties对象
    private static Properties props;

    //定义一个Map,用于存放我们要创建的对象。我们把它称之为容器
    private static Map<String,Object> beans;

    //使用静态代码块为Properties对象赋值
    static {
        try {
            //实例化对象
            props = new Properties();
            //获取properties文件的流对象
            InputStream in = BeanFactory.class.getClassLoader().getResourceAsStream("bean.properties");
            props.load(in);
            //实例化容器
            beans = new HashMap<String,Object>();
            //取出配置文件中所有的Key
            Enumeration keys = props.keys();
            //遍历枚举
            while (keys.hasMoreElements()){
                //取出每个Key
                String key = keys.nextElement().toString();
                //根据key获取value
                String beanPath = props.getProperty(key);
                //反射创建对象
                Object value = Class.forName(beanPath).newInstance();
                //把key和value存入容器中
                beans.put(key,value);
            }
        }catch(Exception e){
            throw new ExceptionInInitializerError("初始化properties失败!");
        }
    }

    /**
     * 根据bean的名称获取对象
     * @param beanName
     * @return
     */
    public static Object getBean(String beanName){
        return beans.get(beanName);
    }

    /**
     * 根据Bean的名称获取bean对象
     * @param beanName
     * @return

    public static Object getBean(String beanName){
        Object bean = null;
        try {
            String beanPath = props.getProperty(beanName);
//            System.out.println(beanPath);
            bean = Class.forName(beanPath).newInstance();//每次都会调用默认构造函数创建对象
        }catch (Exception e){
            e.printStackTrace();
        }
        return bean;
    }*/
}




测试类:


    public static void main(String[] args) {
        //IAccountService as = new AccountServiceImpl();
        for(int i=0;i<5;i++) {
            IAccountService as = (IAccountService) BeanFactory.getBean("accountService");
            System.out.println(as);
            as.saveAccount();
        }

    }


版权说明
文章采用: 《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权。
版权声明:未标注转载均为本站原创,转载时请以链接形式注明文章出处。如有侵权、不妥之处,请联系站长删除。敬请谅解!

这篇文章最后更新于2020-2-14,已超过 1 年没有更新,如果文章内容或图片资源失效,请留言反馈,我们会及时处理,谢谢!
Spring框架的第一天代码用spring框架IOC来解耦总结
« 上一篇
Spring框架的第一天学习知识点pdf下载
下一篇 »

发表评论

HI ! 请登录
注册会员,享受下载全站资源特权。
Array

日历

热门文章