Results 1 to 6 of 6
  1. #1
    kosmos890 is offline Member
    Join Date
    Apr 2012
    Posts
    40
    Rep Power
    0

    Default Interface and ServiceLoader

    I am trying to understand how interfaces and ServiceLoader works.
    In NetBeans I made three Java Application Projects

    1)Dictionary.java (Interface Declaration)
    2)GeneralDictionary.java (Interface implementation)
    Projects Window
    ProjectDictionary (The name of project)
    |
    Source Packages
    |
    META-INF.services
    |
    com.example.dictionary.spi.Dictionary
    (This file contains FQDN of the implementation class
    "com.example.dictionary.GeneralDictionary" )

    3)Main.java

    Interface
    Java Code:
    package com.example.dictionary.spi;
    
    public interface Dictionary {
    
        String getDefinition();
    }
    Implementation
    Java Code:
    package com.example.dictionary;
    
    import com.example.dictionary.spi.Dictionary;
    
    
    public class GeneralDictionary implements Dictionary{
    
        public String getDefinition() {
            return "Hi";
        }
    }
    Main
    Java Code:
    package dictionarydisplayerproject;
    
    import com.example.dictionary.spi.Dictionary;
    import java.util.ServiceLoader;
    
    public class Main {
    
        public static void main(String[] args) {
           
            for(Dictionary d : ServiceLoader.load(Dictionary.class)){
                System.out.print(d.getDefinition());
            }
        }
    
    }
    I run Main but I don't see "Hi"

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Interface and ServiceLoader

    What are you expecting the load() method to return that's useful in a for loop?
    Last edited by Norm; 08-27-2012 at 09:30 PM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    kosmos890 is offline Member
    Join Date
    Apr 2012
    Posts
    40
    Rep Power
    0

    Default Re: Interface and ServiceLoader

    @Norm thanks for your reply.
    I am waiting instances implementations of Dictionary to call getDefinition()

    Sorry for my bad english, it is not my native language , I hope to be understood

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Interface and ServiceLoader

    Read the API doc for the load() method. What does it return? What would that value return in an enhanced for loop?

    Have you read this: http://java.sun.com/developer/techni...ble/index.html
    Last edited by Norm; 08-27-2012 at 10:37 PM.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    kosmos890 is offline Member
    Join Date
    Apr 2012
    Posts
    40
    Rep Power
    0

    Default Re: Interface and ServiceLoader

    @Norm thanks!
    I was afraid that I was wrong with META-INF file.
    Java Code:
    package dictionarydisplayerproject;
    
    import com.example.dictionary.spi.Dictionary;
    import java.util.Iterator;
    import java.util.ServiceLoader;
    
    public class Main {
    
        public static void main(String[] args) {
            // TODO code application logic here
    
            ServiceLoader<Dictionary> sl=ServiceLoader.load(Dictionary.class);
    
            Iterator<Dictionary> dictionaries=sl.iterator();
    
            while(dictionaries.hasNext()){
    
                Dictionary d=dictionaries.next();
    
                System.out.print(d.getDefinition());
            }
        }
    }

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Interface and ServiceLoader

    Is that working now? I've never used the ServiceLoader class.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: 07-08-2012, 05:23 PM
  2. Replies: 0
    Last Post: 11-03-2010, 02:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •