Results 1 to 10 of 10

Thread: @autowired in abstract class

  1. #1

    Default @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. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    You cannot instantiate abstract classes and this spring will not scan/detect them.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3

    Default

    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

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5

    Default

    Quote 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

    Quote 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?

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    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
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  7. #7

    Default

    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)

  8. #8
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    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...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  9. #9

    Default

    Quote 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.

  10. #10
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

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