• 框架 小编搭建


    其实所谓的框架就是通过一些配置文件来将需要运行的模块以及类、方法在软件启动的时候自动运行。如果将需要运行类以及模块配置在文件中那么便于后期的一个维护。

    1.创建一个配置文件如下

     1 class=Service.UserService

    2 method=autoRun

    3 value=唐僧,孙悟空  

    2.    创建两个实现接口的服务类
     1  2 UserService.java
     3 public class UserService implements Service{
     4    // 提供自动运行的方法
     5     public void autoRun(String names){
     6         // 使用,号切割用户列表
     7         String [] ns = names.split(",");
     8         // 遍历
     9         for(String name:ns){
    10             System.out.println("姓名: "+name);
    11         }
    12     } 
    13 }
    14 StartService.java
    15 public class StartService implements Service {
    16    // 提供自动运行的方法
    17     public void autoRun(String names){
    18         // 使用,号切割用户列表
    19         String [] ns = names.split(",");
    20         // 遍历
    21         for(String name:ns){
    22             System.out.println(name);
    23         }
    24     } 
    25 }
    26 以上的两个类拥有共同的方法因此可以抽取为接口
    27 Service.java
    28 public interface Service {
    29     // 提供自动运行的方法
    30     public abstract void autoRun(String names);
    31 }

    3.直接编写一个Main.java进行逻辑处理

     1     public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
     2         Properties properties=new Properties();
     3         properties.load(new BufferedReader(new FileReader(new File("configure.ini"))));
     4         String str_class=(String) properties.getProperty("class");
     5         String str_method=(String)properties.getProperty("method");
     6         String str_value=(String)properties.getProperty("value");
     7         System.out.println(str_class);
     8         System.out.println(str_method);
     9         System.out.println(str_value);
    10         for(Map.Entry e:properties.entrySet())
    11         {
    12             System.out.println(e.getKey()+"   "+e.getValue());
    13         }
    14         Class clazz=Class.forName(str_class);
    15         Constructor con = clazz.getConstructor();
    16         Service service=(Service) con.newInstance();
    17         Method method=clazz.getMethod(str_method,String.class);
    18         service.autoRun(str_value);
    19     }

    参考人家的事例中文解释

     1 public static void main(String[] args) throws Exception{
     2       // 1. 获取配置信息
     3       Properties ps = new Properties();
     4       // 2. 装载文件数据
     5       ps.load(new FileInputStream(new File("jnb.ini")));
     6       // 3. 获取数据
     7       String str_clazz = ps.getProperty("run");
     8       String str_method = ps.getProperty("me");
     9       String str_value = ps.getProperty("value");
    10       // 4. 反射获取Class对象
    11       Class clazz = Class.forName(str_clazz);
    12       // 5. 创建对象
    13       Constructor con = clazz.getConstructor(null);
    14       Service service = (Service)con.newInstance(null);
    15       // 6. 调用相关的方法
    16       Method method = clazz.getMethod(str_method, String.class);
    17       method.invoke(service, str_value);
    18 }

    这样的话就基本的实现了一个框架的模拟,大家以后就可以同配置文件的形式修改程序运行的服务类了。

  • 相关阅读:
    我很喜欢玩游戏,那么我就适合做游戏程序员吗?
    宁可多花1000元租房,也绝不要去挤半小时地铁
    996 盛行的年代,互联网人如何平衡工作和生活 ?
    互联网公司里都有哪些潜规则?
    那些拼命加班的程序员们,后来都怎么样了?
    MongoDB更需要好的模式设计 及 案例赏析
    MongoDB 提升性能的18原则(开发设计阶段)
    关于MongoDB数据库的日志解析
    实现MongoDB读写分离的“读偏好”介绍
    MongoDB分片 在部署和维护管理 中常见事项的总结
  • 原文地址:https://www.cnblogs.com/friends-wf/p/3720521.html
Copyright © 2020-2023  润新知