Announcement Announcement Module
Collapse
No announcement yet.
What is the @Service annotation ? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • What is the @Service annotation ?

    I could not find from the source/docs how it differs from @Component and what's its meant to do...

    Cheers,
    Waldek

  • #2
    As far as I understand it, it's (at the moment) exactly the same as @Component.

    Comment


    • #3
      Originally posted by dejanp View Post
      As far as I understand it, it's (at the moment) exactly the same as @Component.
      Yes, that's correct: at the moment, @Service serves as a specialization of @Component, allowing for implementation classes to be autodetected through classpath scanning. What this means is that you could annotate your service-layer classes with @Component, but by annotating them with @Service instead, your classes are more properly suited for processing by tools or associating with aspects, since @Service makes an ideal target for pointcuts. Of course, it is possible that @Service may carry additional semantics in the future; thus, if you are making a decision between using @Component and @Service, @Service is clearly the better choice for your service-layer.

      Hope this clarifies things a bit.

      Sam

      Comment


      • #4
        But this annotations couples the implementation more tightly to Spring. What are your idea's about that?

        ps: I like the @Service annotation, perfect piece of metadata. The problem I have is with the Spring coupling.

        Comment


        • #5
          Originally posted by Alarmnummer View Post
          But this annotations couples the implementation more tightly to Spring. What are your idea's about that?

          ps: I like the @Service annotation, perfect piece of metadata. The problem I have is with the Spring coupling.
          Don't use it?

          Comment


          • #6
            Originally posted by Alarmnummer View Post
            But this annotations couples the implementation more tightly to Spring. What are your idea's about that?

            ps: I like the @Service annotation, perfect piece of metadata. The problem I have is with the Spring coupling.
            You do not have to use the Spring stereotypes (i.e., @Component, @Service, @Controller, and @Repository or a custom annotation which is itself annotated with @Component as a meta-annotation) to achieve auto-detection of components via classpath scanning. These stereotype annotations are provided merely as a convenience. Thus, the coupling to Spring that you mentioned is actually unnecessary.

            Please see the "Using filters to customize scanning" section of the Spring Reference Manual for details. In summary, you can define filters to include your own annotations, packages, classes, etc.

            You might also be interested in Solomon Duskis' "Guice and Spring 2.5" blog entry, which demonstrates some of these configuration options in an interesting manner.

            Cheers,

            Sam

            Comment


            • #7
              Originally posted by Sam Brannen View Post
              You do not have to use the Spring stereotypes (i.e., @Component, @Service, @Controller, and @Repository or a custom annotation which is itself annotated with @Component as a meta-annotation) to achieve auto-detection of components via classpath scanning. These stereotype annotations are provided merely as a convenience. Thus, the coupling to Spring that you mentioned is actually unnecessary.

              Please see the "Using filters to customize scanning" section of the Spring Reference Manual for details. In summary, you can define filters to include your own annotations, packages, classes, etc.

              You might also be interested in Solomon Duskis' "Guice and Spring 2.5" blog entry, which demonstrates some of these configuration options in an interesting manner.

              Cheers,

              Sam
              Thanks for the reference.

              It's true. Spring doesn't force you to couple unless you really want to. This is probably the biggest advantage that Spring annotation handling over other IoC annotation processrs.

              Comment


              • #8
                I must admit I don't understand the fear of Spring "coupling". It's an implementation you have to annotate, and every implementation is going to have some dependencies. You do not expose these implementations to public anyway, so I don't see the problem really.

                Comment


                • #9
                  I am using the @Service and the @Controller annotations. But these are not getting detected in the JConsole and I do not get my spring application displayed at all in JConsole if I use these. Are these annotations not used for performance monitoring purposes?

                  Comment


                  • #10
                    Coupling to Spring

                    Originally posted by dejanp View Post
                    I must admit I don't understand the fear of Spring "coupling". It's an implementation you have to annotate, and every implementation is going to have some dependencies. You do not expose these implementations to public anyway, so I don't see the problem really.
                    You dont have to worry about coupling if your service class implements a service interface.. then you can provide two versions of the service class, one marked @Service with transactional boundries @Transactional (if you choose) and another class that doesn't have any annotations.. all the guts go in a base class which either the Spring version or other verison extends.. then you wire in the service you want. if you are using spring, you wire in the spring version. if not, use the other. We do this to provide a JTA service and NonJTA service. Spring becomes the factory in this case, otherwise you just create a factory yourself and request which version of the service class u want.

                    Comment

                    Working...
                    X