Java模块服务的使用

 

 

package com.iot1931;

/**
 * FileName: MyService
 * Author:   lps
 * Date:     2022/4/12 12:54
 * Sign:刘品水 Q:1944900433
 */
public interface MyService {
  void service();
}
package com.iot1931.impl;

import com.iot1931.MyService;

/**
 * FileName: czie
 * Author:   lps
 * Date:     2022/4/12 12:55
 * Sign:刘品水 Q:1944900433
 */
public class Czie implements MyService {
    @Override
    public void service() {
        System.out.println("常州工程 物联网1913刘品水");
    }
}
package com.iot1931.impl;

import com.iot1931.MyService;

/**
 * FileName: itlps
 * Author:   lps
 * Date:     2022/4/12 12:55
 * Sign:刘品水 Q:1944900433
 */
public class Itlps implements MyService {
    @Override
    public void service() {
        System.out.println("坚持就是胜利 刘品水");
    }
}
import com.iot1931.MyService;
import com.iot1931.impl.Czie;

/**
* FileName: module-info
* Author: lps
* Date: 2022/4/12 12:28
* Sign:刘品水 Q:1944900433
*/module myOne {
exports com.iot1913;
exports com.iot1921;
exports com.iot1931;

//provides MyService with Itlps;
provides MyService with Czie;
}

import com.iot1931.MyService;

/**
* FileName: module-info
* Author: lps
* Date: 2022/4/12 12:28
* Sign:刘品水 Q:1944900433
*/module myTwo {
requires myOne;
uses MyService;
}
package iot1913;

import com.iot1931.MyService;

import java.util.ServiceLoader;

/**
 * FileName: Test02
 * Author:   lps
 * Date:     2022/4/12 13:03
 * Sign:刘品水 Q:1944900433
 */
public class Test02 {
    public static void main(String[] args) {
        //加载服务
        // ServiceLoader<CodecFactory> loader = ServiceLoader.load(CodecFactory.class);
        // for (CodecFactory factory : loader) { Encoder enc = factory.getEncoder("PNG");
        // if (enc != null) ... use enc to encode a PNG file break; }
        //上面这个是帮助文档里的范例
        ServiceLoader<MyService> myServices = ServiceLoader.load(MyService.class);
        //遍历服务
        for (MyService my:myServices){
            my.service();
        }
    }
}

 

posted @ 2022-04-12 13:39  刘品水  阅读(47)  评论(0编辑  收藏  举报