zoukankan      html  css  js  c++  java
  • Spring学习:程序的耦合和解耦的思路分析

    程序的耦合

      耦合:程序间的依赖关系

        包括:

          类之间的依赖

          方法间的依赖

      解耦:

        降低程序间的依赖关系

      在实际开发中:

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

      解耦思路:

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

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


    创建BeanFactory  

     1 /**
     2  * 一个创建Bean对象的工厂
     3  *
     4  * Bean:在计算机英语中,有可重用组件的含义
     5  * JavaBean:用java语言编写的可重用组件
     6  *          javabean > 实体类
     7  *
     8  *    它就是创建我们的service的dao对象的
     9  *    第一个:需要一个配置文件来配置我们的service和dao
    10  *          配置的内容:唯一标识=全限定类名
    11  *    第二个:通过读取配置文件中配置的内容,反射创建对象
    12  *
    13  *    配置文件可以是xml也可以是properties
    14  *
    15  */
    16 public class BeanFactory {
    17 
    18     //定义一个Properties对象
    19     private static Properties props;
    20 
    21     //使用静态代码块为Properties对象赋值
    22     static {
    23         try {
    24             //实例化对象
    25             props = new Properties();
    26             //获取properties文件的流对象,创建在resources文件夹下的properties文件会成为类根路径下的文件,不用写包名
    27             InputStream in = BeanFactory.class.getClassLoader().getResourceAsStream("bean.properties");
    28             props.load(in);
    29         } catch (Exception e) {
    30             throw new ExceptionInInitializerError("初始化properties失败");
    31         }
    32     }
    33 
    34     /**
    35      * 根据Bean的名称获取bean对象
    36      * @param beanName
    37      * @return
    38      */
    39     public static Object getBean(String beanName){
    40         Object bean = null;
    41         try{
    42             String beanPath = props.getProperty(beanName);
    43             System.out.println(beanPath);
    44             bean = Class.forName(beanPath).newInstance();
    45         }catch (Exception e){
    46             e.printStackTrace();
    47         }
    48         return bean;
    49     }
    50 
    51 }

    调用BeanFactory,反射创建对象

     1 /**
     2  * 账户业务层实现类
     3  */
     4 public class AccountServiceImpl implements IAccountService {
     5 
     6     //private IAccountDao accountDao = new AccountDaoImpl();
     7     private IAccountDao accountDao = (IAccountDao) BeanFactory.getBean("accountDao");
     8 
     9     public void saveAccount(){
    10         accountDao.saveAccount();
    11     }
    12 }
     1 /**
     2  * 模拟一个表现层,用于调用业务层
     3  */
     4 public class Client {
     5 
     6     public static void main(String[] args) {
     7 //        IAccountService accountService = new AccountServiceImpl();
     8         IAccountService accountService = (IAccountService) BeanFactory.getBean("accountService");
     9         accountService.saveAccount();
    10     }
    11 }

     

  • 相关阅读:
    JUC原子类 1
    线程优先级和守护线程
    多线程中断
    关于html5不支持frameset的解决方法
    shell中$0,$?,$!等的特殊用法
    Linux GCC常用命令
    C/C++中extern关键字详解
    js实现iframe自适应高度
    java线程安全总结
    Linux平台Java调用so库-JNI使用例子
  • 原文地址:https://www.cnblogs.com/flypig666/p/11508214.html
Copyright © 2011-2022 走看看