Wednesday, November 19, 2008

[Spring] @Autowired doesn't work in an abstract class

If your @AutoWired doesn't seem to work in an abstract class, it could be because you need to add @Component to the solid class that extends the abstract class.

So if you have an abstract class:
package com.maaloe.test;

import org.springframework.beans.factory.annotation.Autowired;

public abstract class MyAbstractClass {
  @Autowired
  TestService testService;
  public void test() {
    System.out.println("TestService: " + testService.myToString());
  }
}

and a solid class:
package com.maaloe.test;

import org.springframework.stereotype.Component;

@Component
public class MySolidClass extends MyAbstractClass {
  public void mytest() {
    super.test();
  }
}

Note that this abstract class does NOT have a @Component tag, as this is never used as a solid class, so even though it uses @Autowired for injecting its member variables, only the class extending the MyAbstractClass need to have the @Component tag.

I found this rather confusing... but that's apparently the way it works. :)

Heres' the TestService and the TestServiceImpl:
package com.maaloe.services;

public interface TestService {
  String myToString();
}

package com.maaloe.services.impl;

import org.springframework.stereotype.Component;

@Component
public class TestServiceImpl implements TestService {
  public String myToString() {
    return "TestService ping...";
  }
}

and finally, the applicationContext must contain:

<context:component-scan base-package="com.maaloe" />
<context:annotation-config/>


Enjoy...

Note that none of the above have been compiled, it's just written from my memory, so forgive me if it contains minor errors.

Please comment if you have anything to add!

9 comments:

Guido said...

Thank you.

camposer said...

Gracias!

sheela rajesh said...

Thanks first of all for the useful info.the idea in this article is quite different and innovative please update more.
JAVA Training in Chennai
Best JAVA Training institute in Chennai
Python Training in Chennai
Selenium Training in Chennai
Android Training in Chennai
Big data training in chennai
JAVA Training in Annanagar
java training in chennai anna nagar

Reshma said...

Admire this blog. Keep sharing more updates like this
Tally Course in Chennai
Tally Training in Chennai
Tally training in coimbatore
Tally course in madurai
Tally Course in Hyderabad
Tally Training in Bangalore
Tally classes in coimbatore
Tally coaching centre in coimbatore
Tally course in marathahalli
German Classes in Bangalore

greatrockdev said...

Howdy! I simply wish to offer you a big thumbs up for your great info you have got here on this post. I'll be returning to your blog for more soon.
Click here to getMoreinformation.

Revathi said...

Informative post,It is useful for me to clear my doubts.I hope others also like the information you gave in your blog.keep it up!!

android training in chennai

android online training in chennai

android training in bangalore

android training in hyderabad

android Training in coimbatore

android training

android online training

Links For You said...

Read Basic Wifi Hacking by Mad76e with a free trial. Read millions of eBooks and audiobooks on the web, iPad, iPhone and Android. Online Wifi Hacker

Samia Beel said...

ShareMouse allows you to share one mouse and keyboard with multiple Windows and Mac computers. You just need to move the mouse pointer ShareMouse Key

Hi Every One said...

“Yesterday brought the beginning, tomorrow brings the end, though somewhere in the middle we became the best of friends.” Unknown. “Goodbyes make you think. Best Farewell Quotes For Friends