Announcement Announcement Module
Collapse
No announcement yet.
@autowired in abstract class Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @autowired in abstract class

    Hi everyone,
    I don't understand why the code below does not work. It does work when I delete "abstract".
    Is there a way to have my configurationService autowired in this abstract class?
    (configurationService has to be static so I cannot apply the autowired to it directly)

    Code:
    @Component
    public abstract class ControllerUtils implements Constants{
    
    	/** The configuration service. */
    	private static IConfigurationService configurationService;
    
    	@Autowired
    	private void setConfigurationService(
    			IConfigurationService configurationService) {
    		ControllerUtils.configurationService = configurationService;
    	}
    
    	...
    }

  • #2
    You cannot instantiate abstract classes and this spring will not scan/detect them.

    Comment


    • #3
      I know that I can not instantiate an abstract class but have a look at the last post of this thread
      http://forum.springsource.org/showthread.php?t=55138

      Comment


      • #4
        Then why on earth put @Component on that class.

        You put the annotation on a private class which isn't visible when scanning the actual class which extends this one...

        If you remove abstract spring is going to instantiate a ControllerUtils for you scans it and detects the method, due to the fact that it is a static variable it looks like it gets injected. If you would check your ApplicationContext you will see 1 additional bean when you remove the abstract keyword.

        Comment


        • #5
          Originally posted by Marten Deinum View Post
          If you remove abstract spring is going to instantiate a ControllerUtils for you scans it and detects the method, due to the fact that it is a static variable it looks like it gets injected. If you would check your ApplicationContext you will see 1 additional bean when you remove the abstract keyword.
          I know

          Originally posted by Marten Deinum View Post
          Then why on earth put @Component on that class.
          You put the annotation on a private class which isn't visible when scanning the actual class which extends this one...
          and this class won't be abstract if I follow your logic(yes), so in the end I have a new bean too. Is it really better?

          Comment


          • #6
            Simply put spring can only inject classes it creates instances from. You cannot (and neither can spring) instantiate abstract classes so dependencies will never be injected.

            The thread you linked to showed a sample with 2 subclasses.


            Solutions/workarounds
            1. create a subclass and make the method protected/public
            2. remove abstract (you could make it final)
            3. create a FactoryBean which injects the dependency and leave all as is

            Comment


            • #7
              i'll try your way later (right now I cannot).
              but in the thread I have linked, the class Base isn't a subclass so I'm wondering if simply having a subclass ("@component"-ed) with nothing in it wouldn't work.
              (I can also have only the setter)

              Comment


              • #8
                Base isn't but that is the abstract class Sub is the sub class of Base (I really suggest you read again). The @Component on the class Base is confusing and useless.

                so I'm wondering if simply having a subclass ("@component"-ed) with nothing in it wouldn't work.
                Sigh... Java semantics still apply. Your setter is private the one in the sample is public. a private method isn't becoming part of the subclass so it isn't detected when scanning the subclass...

                Comment


                • #9
                  Originally posted by Marten Deinum View Post
                  Base isn't but that is the abstract class Sub is the sub class of Base (I really suggest you read again). The @Component on the class Base is confusing and useless.
                  I did said the same but you said there are 2 subclasses (where I see only one ).

                  And of course if I do some inheritance I ll check my private functions to maybe become protected.

                  Comment


                  • #10
                    Mybad, accidently read that one of the other classes also extended it.

                    Regarding the private stuff, I'm reconsidering my earlier sayings. It also works if you put @Autowired on a private field so it might also work on a private setter. Haven't tried it, but the internals of spring do a best effort so it even might work.

                    Comment

                    Working...
                    X