Results 1 to 6 of 6

Thread: List all Services

  1. #1
    Join Date
    Mar 2012
    Posts
    3

    Default List all Services

    Hello.

    Am using the ServletDispatcher to load some controllers, who are all sharing some services lots of those services are in the singleton scope. And are basicly used to encapsulate dataaccess to some proprietary datasources.

    Now, I would in a controlle like to get access to all Services who implements a specific interface?
    (Does this question even make sense?)

    And, thanks for your time.

  2. #2

    Default

    I guess I dont understand why you would want all the implementations...

    seems to me you would only need one implementation in your controller.

    @Autowired
    @Qualifier("beanIdHere")
    MyInterface myInterface;


    Now if you wanted to you could even autowire in the context and call getBean() to get the implementation you needed.
    @Autowired
    ApplicationContext context;

    Or you could do what I showed first and autowire in all the implementations, but I really don't understand why you would want to do this, from what you are saying it almost sounds like you need to to create a factory class that creates and returns the correct implemtation for you. You can take di further to decouple the factory class from the service. Just google for 'factoryclass spring' for some examples.

    Thanks,

  3. #3
    Join Date
    Mar 2012
    Posts
    3

    Default Sorry for being unclear-

    Or you could do what I showed first and autowire in all the implementations, but I really don't understand why you would want to do this, from what you are saying it almost sounds like you need to to create a factory class that creates and returns the correct implemtation for you. You can take di further to decouple the factory class from the service. Just google for 'factoryclass spring' for some examples.

    Thanks,
    I have not been clear in my question, sorry for that.

    I do not care about where it is used, I care if it is being used

    per ex. I got some classes looking like:

    @Scope( value = "singleton" )
    @Service
    public class someService implements whatever, aCapablility

    Binding them to my controllers using the @Autowire Annotation. Pretty cool and simple.

    My problem is now, I would really like to find all, allready existing instances of any services ( ie classes with Scope singleton and the @Service annotation ) that is in the current container.

    Is is possible?

  4. #4

    Default

    First off you don't need to specify singleton, as it is the default. And a singleton by definition only has one instance. When you @Autowire your service in you are getting the single instance of the service that spring created for you when it initialized the context.

  5. #5
    Join Date
    Mar 2012
    Posts
    3

    Default Got that one

    I am with you so for ( And scope singleton is nice for the explicity )

    But what I try to do is in a generic way to fetch all exising singletons without knowing the type.

    Main idea is to be able to monitor services

    something like

    @Autowired
    ApplicationContext context;
    void someMethod()
    {
    for ( Map.Entry< String, Object > b : context.getBeansWithAnnotation ( Service.class )
    .entrySet () )
    {
    // Playing around with the stuff I got as services.
    }

  6. #6

    Default

    Last edited by wgorder; Mar 16th, 2012 at 08:37 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
  •