Announcement Announcement Module
Collapse
No announcement yet.
List all Services Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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,

    Comment


    • #3
      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?

      Comment


      • #4
        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.

        Comment


        • #5
          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.
          }

          Comment


          • #6
            have a look at this

            http://static.springsource.org/sprin....lang.Class%29
            Last edited by wgorder; Mar 16th, 2012, 09:37 PM.

            Comment

            Working...
            X